hdu 2222(AC自动机模板)
2010-05-13 14:06
573 查看
#include <cstdio> #include <cstring> #include <queue> using namespace std; const int size=26; char key[55],dir[1000005]; class ac_auto { public: ac_auto *fail; ac_auto *next[size]; int cnt; ac_auto() { fail=NULL; cnt=0; memset(next,NULL,sizeof(next)); }; void Insert(ac_auto *root,char *str) //构造trie树 { int i=0,index; ac_auto *tmp=root; while(str[i]) { index=str[i]-'a'; if(tmp->next[index]==NULL) tmp->next[index]=new ac_auto(); tmp=tmp->next[index]; i++; } tmp->cnt++; //单词结尾,计数加1 }; void build(ac_auto *root) //构造自动机 { queue<ac_auto*> que; int i; ac_auto *tmp,*p; root->fail=NULL; que.push(root); while(!que.empty()) { tmp=que.front(); que.pop(); p=NULL; for(i=0;i<size;++i) { if(tmp->next[i]!=NULL) { if(tmp==root) tmp->next[i]->fail=root; else { p=tmp->fail; while(p!=NULL) { if(p->next[i]!=NULL) { tmp->next[i]->fail=p->next[i]; break; } p=p->fail; } if(p==NULL) tmp->next[i]->fail=root; } que.push(tmp->next[i]); } } } }; int query(char *str,ac_auto *root) //查询,返回之前构造的树中有多少字串出现在str中 { int i=0,count=0,index,len=strlen(str); ac_auto *tmp=root,*t; while(str[i]) { index=str[i]-'a'; while(tmp->next[index]==NULL && tmp!=root) tmp=tmp->fail; tmp=tmp->next[index]; if(tmp==NULL) tmp=root; t=tmp; while(t!=root) { cnt+=t->cnt; t->cnt=0; t=t->fail; } i++; } return cnt; }; }; int main() { int cas,n; scanf("%d",&cas); while(cas--) { ac_auto *root = new ac_auto(); scanf("%d",&n); while(n--) { scanf("%s",key); root->Insert(root,key); } root->build(root); scanf("%s",dir); printf("%d/n",root->query(dir,root)); } return 0; }
相关文章推荐
- hdu 2222 Keywords Search(AC自动机模板)
- HDU 2222 Keywords Search(AC自动机模板)
- HDU 2222 Keywords Search 【AC自动机模板】
- hdu 2222 AC自动机(可做模板)
- hdu 2222 ac自动机 模板
- HDU 2222 Keywords Search 【AC自动机模板】
- HDU 2222 Keywords Search(我的第一道AC自动机,模板题)
- HDU 2222 ACAM模板(AC自动机)
- hdu 2222 AC自动机模板(非指针)
- AC自动机 模板 hdu 2222
- HDU-2222 Keywords Search (AC自动机模板)
- HDU 2222 Keywords Search (AC自动机模板)
- AC自动机(Aho-Corasick automation)模板 HDU:2222
- CUGB专题训练之数据结构:E - Keywords Search(HDU 2222 AC自动机经典入门模板题)
- HDU 2222 ac自动机入门模板题
- HDU 2222 ac自动机模板题
- hdu 2222 ac自动机模板
- HDU 2222 AC自动机模板
- 【AC自动机详解+入门模板】HDU 2222
- hdu 2222 ac自动机入门题 可以做模板