hdu 3065 AC自动机(指针+数组)
2015-08-27 17:54
429 查看
病毒侵袭持续中
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8753 Accepted Submission(s): 3078
Problem Description
小t非常感谢大家帮忙解决了他的上一个问题。然而病毒侵袭持续中。在小t的不懈努力下,他发现了网路中的“万恶之源”。这是一个庞大的病毒网站,他有着好多好多的病毒,但是这个网站包含的病毒很奇怪,这些病毒的特征码很短,而且只包含“英文大写字符”。当然小t好想好想为民除害,但是小t从来不打没有准备的战争。知己知彼,百战不殆,小t首先要做的是知道这个病毒网站特征:包含多少不同的病毒,每种病毒出现了多少次。大家能再帮帮他吗?
Input
第一行,一个整数N(1<=N<=1000),表示病毒特征码的个数。
接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。
在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。
Output
按以下格式每行一个,输出每个病毒出现次数。未出现的病毒不需要输出。
病毒特征码: 出现次数
冒号后有一个空格,按病毒特征码的输入顺序进行输出。
Sample Input
3 AA BB CC ooxxCC%dAAAoen....END
Sample Output
AA: 2 CC: 1 Hint Hit: 题目描述中没有被提及的所有情况都应该进行考虑。比如两个病毒特征码可能有相互包含或者有重叠的特征码段。 计数策略也可一定程度上从Sample中推测。
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <algorithm> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <ctime> using namespace std; #pragma comment(linker,"/STACK:102400000,102400000") /// kuo zhan #define clr(s,x) memset(s,x,sizeof(s)) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lowbit(x) (x&(-x)) #define PB push_back #define For(i,a,b) for(int i=a;i<b;i++) #define FOR(i,a,b) for(int i=a;i<=b;i++) typedef long long LL; typedef unsigned int uint; typedef unsigned long long ULL; typedef vector<int> vint; typedef vector<string> vstring; void RI (int& x){ x = 0; char c = getchar (); while (c == ' '||c == '\n') c = getchar (); bool flag = 1; if (c == '-'){ flag = 0; c = getchar (); } while (c >= '0' && c <= '9'){ x = x * 10 + c - '0'; c = getchar (); } if (!flag) x = -x; } void RII (int& x, int& y){RI (x), RI (y);} void RIII (int& x, int& y, int& z){RI (x), RI (y), RI (z);} const double PI = acos(-1.0); const int maxn = 2e6 + 100; const int maxm = 55; const int kind = 26; const int maxq = 5e5 + 100; const LL mod = 1e9 + 7; const double eps = 1e-9; const int INF = 0x7fffffff; /************************************END DEFINE*********************************************/ struct node{ node *fail; node *nxt[kind]; int cnt,id; node(){ cnt = 0;id = 0;fail = NULL; For(i,0,kind)nxt[i]=NULL; } }*q[maxq]; int head,tail; char t[1005][maxm]; char s[maxn]; int vis[1005]; void add(char *word,node *root,int no){ node * p =root; int i = 0; while(word[i]){ int idx = word[i]-'A'; if(p->nxt[idx]==NULL)p->nxt[idx]=new node(); p=p->nxt[idx]; i++; } p->cnt++; p->id=no; } void build_ac(node *root){ node *p = NULL; q[head++]=root; while(head!=tail){ node *tmp = q[tail++]; For(i,0,kind){ if(tmp->nxt[i]!=NULL){ if(tmp==root)tmp->nxt[i]->fail=root; else{ p=tmp->fail; while(p!=NULL){ if(p->nxt[i]!=NULL){ tmp->nxt[i]->fail=p->nxt[i]; break; } p=p->fail; } if(p==NULL)tmp->nxt[i]->fail=root; } q[head++]=tmp->nxt[i]; } } } } void query(node *root){ int i = 0,idx; node *p = root; while(s[i]){ if(s[i]>='A'&&s[i]<='Z'){ idx = s[i] - 'A'; while(p->nxt[idx]==NULL && p!=root)p=p->fail; p=p->nxt[idx]; p=(p==NULL)?root:p; node *tmp = p; while(tmp != root && tmp->cnt>0){ vis[tmp->id]++; tmp=tmp->fail; } } else p=root; i++; } } int main() { int n; while(~scanf("%d",&n)){ clr(vis,0); node *root = new node(); head = tail = 0; For(i,0,n){ scanf("%s",t[i]); add(t[i],root,i+1); } build_ac(root); scanf("%s",s); query(root); FOR(i,1,n){ if(vis[i]) printf("%s: %d\n",t[i-1],vis[i]); } } return 0; }
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <algorithm> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <ctime> using namespace std; #pragma comment(linker,"/STACK:102400000,102400000") /// kuo zhan #define clr(s,x) memset(s,x,sizeof(s)) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lowbit(x) (x&(-x)) #define PB push_back #define For(i,a,b) for(int i=a;i<b;i++) #define FOR(i,a,b) for(int i=a;i<=b;i++) typedef long long LL; typedef unsigned int uint; typedef unsigned long long ULL; typedef vector<int> vint; typedef vector<string> vstring; void RI (int& x){ x = 0; char c = getchar (); while (c == ' '||c == '\n') c = getchar (); bool flag = 1; if (c == '-'){ flag = 0; c = getchar (); } while (c >= '0' && c <= '9'){ x = x * 10 + c - '0'; c = getchar (); } if (!flag) x = -x; } void RII (int& x, int& y){RI (x), RI (y);} void RIII (int& x, int& y, int& z){RI (x), RI (y), RI (z);} /************************************END DEFINE*********************************************/ const int maxn = 50010; const int kind = 128; int idx(char c){return (int)c;} int chd[maxn][kind],sz,val[maxn],fail[maxn],last[maxn]; void init(){ sz=1; memset(chd[0],0,sizeof(chd[0])); memset(val,0,sizeof(val)); memset(fail,0,sizeof(fail)); memset(last,0,sizeof(last)); } void add(char *p,int id){ int u=0; int len = strlen(p); for(int i=0;i<len;i++){ int c = idx(p[i]); if(chd[u][c]==0){ memset(chd[sz],0,sizeof(chd[sz])); chd[u][c]=sz++; } u=chd[u][c]; } val[u]=id; } int getFail(){ queue<int> q; fail[0]=0; for(int c=0;c<kind;c++){ int u=chd[0][c]; if(u){ fail[u]=0;q.push(u);last[u]=0; } } while(!q.empty()){ int r=q.front(); q.pop(); for(int c=0;c<kind;c++){ int u=chd[r][c]; if(!u){ chd[r][c]=chd[fail[r]][c]; continue; } q.push(u); int v=fail[r]; while(v&&!chd[v][c]) v=fail[v]; fail[u]=chd[v][c]; last[u]=(val[fail[u]])?fail[u]:last[fail[u]]; } } } int num[1005]; void query(char *T){ int len = strlen(T), j = 0; for(int i = 0; i < len; i++){ int c = idx(T[i]); while(j && chd[j][c]==0) j = fail[j]; j = chd[j][c]; int temp = j; while(temp && val[temp]){ num[val[temp]]++; //val[temp]=0; temp = fail[temp]; } } } char t[1005][55]; char s[2000005]; int main() { int n; while(scanf("%d",&n)!=EOF){ init(); clr(num,0); for(int i=1;i<=n;i++){ scanf("%s",t[i]); add(t[i],i); } getFail(); scanf("%s",s); query(s); for(int i=1;i<=n;i++){ if(num[i]){ printf("%s: %d\n",t[i],num[i]); } } } return 0; }
相关文章推荐
- /etc/hosts文件修改后如何生效
- 如何使用MVP架构Android应用项目
- Python计算矩阵相乘
- 使用非监督学习技术分析情感(IEEE2013)
- hdu 1009 FatMouse' Trade(贪心)
- Spectral Clustering(学习Free Mind知识整理)
- JS 事件
- ALGLIB
- 1283 最简单的计算机【简单模拟】
- java学习日记2
- amaze UI animation动画示例
- 关于/etc/hosts文件
- Python中dict详解
- SVM(二)拉格朗日对偶问题
- Java NIO API详解2
- JSON字符串对应实体类
- ZooKeeper 特性
- 使用Xcode、Android Studio将项目链接到Git
- HDU3072 Intelligence System(强连通+最小传递费用+边权)
- cnn下matlab移植opencv体会