Codeforces 633C Spy Syndrome 2(Trie+dp)
来源:互联网 发布:算法竞赛入门经典 微盘 编辑:程序博客网 时间:2024/06/01 20:50
题意:
给你一个长字符串,再给你一些子串,这些子串全部化成小写字母在翻转,可能会组成长字符串,让你输出一种法案。
这题一开就要用的字典树,我已开始以为要AC自动机,然而并不需要。我们只需要用字典树吧子串存下来就好了。然后就会说查找的过程。
查抄的时候,我们用ans[i] 表示第i个位置是标号为ans[i]的子串的末尾,我们只需要按照这个顺序分别找下去就好了,说的不太清楚,代码看看应该能懂。
代码:
//// Created by CQU_CST_WuErli// Copyright (c) 2016 CQU_CST_WuErli. All rights reserved.//#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cctype>#include <cmath>#include <string>#include <vector>#include <list>#include <map>#include <queue>#include <stack>#include <set>#include <algorithm>#include <sstream>#define CLR(x) memset(x,0,sizeof(x))#define OFF(x) memset(x,-1,sizeof(x))#define MEM(x,a) memset((x),(a),sizeof(x))#define BUG cout << "I am here" << endl#define lookln(x) cout << #x << "=" << x << endl#define SI(a) scanf("%d",&a)#define SII(a,b) scanf("%d%d",&a,&b)#define SIII(a,b,c) scanf("%d%d%d",&a,&b,&c)#define rep(flag,start,end) for(int flag=start;flag<=end;flag++)#define Rep(flag,start,end) for(int flag=start;flag>=end;flag--)#define Lson l,mid,rt<<1#define Rson mid+1,r,rt<<1|1#define Root 1,n,1#define BigInteger bignconst int MAX_L=2005;// For BigIntegerconst int INF_INT=0x3f3f3f3f;const long long INF_LL=0x7fffffff;const int MOD=1e9+7;const double eps=1e-9;const double pi=acos(-1);typedef long long ll;using namespace std;int n,m;char s[10000+10];string ts[100010];int ans[10000+10];const int maxn=1e6+10;const int maxv=50;int have[maxn],fail[maxn];int nxt[maxn][maxv];struct ACAutoMaton { int cnt,root; int ID_cnt; map<char,int> ID; void init() { cnt=ID_cnt=0; ID.clear(); root=newNode(); } int newNode() { for (int i=0;i<26;i++) { nxt[cnt][i]=-1; } have[cnt++]=0; return cnt-1; } int getID(char x) { return x-'a'; } void insert(char buf[],int pos) { int len=strlen(buf); int now=root; for (int i=0;i<len;i++) { int id=getID(buf[i]); if (nxt[now][id]==-1) nxt[now][id]=newNode(); now=nxt[now][id]; } have[now]=pos; } void find(int pos) { int now=root; for (;pos<n;pos++) { int id=getID(s[pos]); if (nxt[now][id]==-1) return; now=nxt[now][id]; if (have[now] && !ans[pos]) ans[pos]=have[now]; } return; }}ac;int main(int argc, char const *argv[]) {#ifdef LOCAL freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin); // freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout);#endif while (SI(n)==1) { scanf("%s",s); SI(m); for (int i=1;i<=m;i++) ts[i].clear(); ac.init(); int idx; for (int i=1;i<=m;i++) { idx=i; char t[1010]; scanf("%s",t); for (int j=0;t[j];j++) ts[i]+=t[j]; for (int j=0;t[j];j++) if (t[j]>='A' && t[j]<='Z') t[j]+=32; int len=strlen(t); reverse(t,t+len); ac.insert(t,idx); //插入第i个字符串 } CLR(ans); for (int i=0;i<n;i++) if (i==0 || ans[i-1]) ac.find(i); vector<int> v; int cnt=n-1; while (cnt>=0) { if (ans[cnt]) v.push_back(ans[cnt]); cnt-=ts[ans[cnt]].size(); } for (int i=v.size()-1;i>=0;i--) { cout << ts[v[i]] << ' '; } cout << endl; } return 0;}
0 0
- Codeforces 633C Spy Syndrome 2(Trie+dp)
- codeforces 633C. Spy Syndrome 2 trie + dp
- Codeforces 633C Spy Syndrome 2(Trie+暴力)
- CodeForces 633C Spy Syndrome 2
- Manthan, Codefest 16 (C. Spy Syndrome 2 (trie树 + DP))
- Manthan, Codefest 16 C. Spy Syndrome 2(dp、trie|哈希)
- Codeforces 633C Spy Syndrome 2 【字典树 + DFS】
- Codeforces 633C Spy Syndrome 2 字典树
- Manthan, Codefest 16 C. Spy Syndrome 2(Trie树套DP)
- trie+dp+打印路径 Codeforces633C Spy Syndrome 2
- Manthan, Codefest 16 633C Spy Syndrome 2(dfs + stl)
- Manthan, Codefest 16 -C. Spy Syndrome 2
- CF633C Spy Syndrome 2
- Manthan, Codefest 16 C. Spy Syndrome 2 ★
- 【Manthan, Codefest 16C】【DP SET-MAP 字典树哈希法】Spy Syndrome 2 字符串是否由字典库单词反转加密而成
- codeforces 615 C. Running Track trie + dp
- Codeforces Round #367 (Div. 2) dp,trie
- Codeforces 615C Running Track (Round #338 (Div. 2) C题) Trie + dp
- Error:Execution failed for task ':app:compileDebugJavaWithJavac'.> Compilation failed; see the compi
- 2015年终总结 走向职业化
- 安卓开发:Activity的生命周期
- Ubuntu下彻底卸载重装mysql
- 。。。。。。
- Codeforces 633C Spy Syndrome 2(Trie+dp)
- 如何让你的html button本身居中?------用center或者div的align属性吧
- Android 5.x之Toolbar和Palette
- 《年终总结》——2015
- Mac下搭建myEclipse开发环境
- 4.1.2.2 Service 实现多线程的下载
- iOS 开发中使用block的注意点
- 基于dileber的android网络图片播放器
- Codeforces 633D Fibonacci-ish(搜索)