AC自动机模板 hdu2222
2013-11-09 18:39
393 查看
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <string.h> #include <queue> //AC自动机模板题(动态建立) hdu2222 using namespace std; char a[1000005], b[55]; struct trie { trie *next[26]; trie *fail; //失败指针,类似于kmp中的next int g; trie() { for(int t=0; t<26; ++t) next[t]=NULL; fail=NULL; g=0; } }*root; void create(char *p) { int t=0, k, j=strlen(p); trie *q=root; while(t<j) { k=p[t]-'a'; if(!q->next[k]) { q->next[k]=new trie(); } q=q->next[k]; t++; } q->g++; //若重复也算,则加上去 return ; } void makefail() { queue<trie *> q; q.push(root); while(!q.empty()) { trie *p=q.front(), *temp; q.pop(); for(int t=0; t<26; ++t) { if(p->next[t]) { temp=p; if(p==root) { temp->next[t]->fail=p; } else { while(temp->fail) { if(temp->fail->next[t]) { p->next[t]->fail=temp->fail->next[t]; break; } temp=temp->fail; } if(!temp->fail) { p->next[t]->fail=root; } } q.push(p->next[t]); } } } return ; } int f(char *p) { int t, k, s=0, j=strlen(p); trie *q=root; for(t=0; t<j; ++t) { k=p[t]-'a'; while(q!=root&&q->next[k]==NULL) { q=q->fail; } q=q->next[k]; if(!q)q=root; trie *temp=q; while(temp!=root&&temp->g) { s+=temp->g; //加上重复的 temp->g=0; temp=temp->fail; } } return s; } int main() { int n, t, T; scanf("%d", &T); while(T--) { root=new trie(); scanf("%d", &n); for(t=0; t<n; ++t) { scanf("%s", b); create(b); } makefail(); scanf("%s", a); printf("%d\n", f(a)); } return 0; }
相关文章推荐
- Keywords Search---hdu2222(AC自动机 模板)
- AC自动机 模板 【HDU2222】 Keywords Search
- AC自动机模板(数组+指针)hdu2222
- hdu2222 ac自动机模板
- ac自动机模板(hdu2222)
- HDU2222_Keywords Search _AC自动机模板题
- HDU2222 AC自动机 入门模板
- ac自动机模板(hdu2222)
- AC自动机(1)--hdu2222(基本模板)
- 【HDU2222】【AC自动机模板 测烂为止】Keywords Search
- ac自动机模板hdu2222
- [hdu2222]ac自动机(模板)
- HDU2222 Keywords Search(AC自动机模板)
- HDU2222 Keywords Search [AC自动机模板]
- AC自动机-HDU2222-模板题
- HDU2222 Keywords Search [AC自动机模板]
- hdu2222 ac自动机模板
- AC自动机模板(hdu2222)
- [AC自动机(模板题)] hdu2222 Keywords Search
- AC自动机模板(hdu2222)