hdu2222 AC自动机入门 指针型模板
2013-07-03 20:22
323 查看
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <map> #include <set> #include <algorithm> #include <ctime> #include <vector> #include <string> #include <stack> #include <queue> using namespace std; #define maxnode 10000*100 #define sigma_size 26 struct Node { Node *fail; Node *nxt[sigma_size]; int count; Node() { fail=NULL; memset(nxt,NULL,sizeof(nxt)); count=0; } }*q[maxnode]; void insert(Node *root,char *str) { Node *p=root; for(int i=0;str[i]!='\0';++i) { if(p->nxt[str[i]-'a']==NULL) p->nxt[str[i]-'a']=new Node(); p=p->nxt[str[i]-'a']; } p->count++; } void getfail(Node *root) { Node *u,*t,*p; root->fail=NULL; int head=0,tail=0; q[tail++]=root; while(head<tail) { u=q[head++]; for(int i=0;i<sigma_size;++i) if(u->nxt[i]!=NULL) { if(u==root) u->nxt[i]->fail=root; else { p=u->fail; while(p!=NULL) { if(p->nxt[i]!=NULL) { u->nxt[i]->fail=p->nxt[i]; break; } p=p->fail; } if(p==NULL) u->nxt[i]->fail=root; } q[tail++]=u->nxt[i]; } } } int query(Node *root,char *str) { int res=0; Node *p=root,*temp; for(int i=0;str[i]!='\0';++i) { Node *pre=p; while(p->nxt[str[i]-'a']==NULL && p!=root) p=p->fail; p=p->nxt[str[i]-'a']; if(p==NULL) p=root; //if(p!=root) //p=p->nxt[str[i]-'a']; temp=p; while(temp!=NULL && temp->count!=-1) { res+=temp->count; temp->count=-1; temp=temp->fail; } } return res; } char str[100]; char T[1000500]; int main () { int ncase,n; scanf("%d",&ncase); while(ncase--) { scanf("%d",&n); Node *root=new Node(); for(int i=0;i<n;++i) { scanf("%s",str); insert(root,str); } getfail(root); scanf("%s",T); int ans=query(root,T); printf("%d\n",ans); } return 0; }
相关文章推荐
- HDU2222 AC自动机 入门模板
- hdu 2222 ac自动机入门题 可以做模板
- HDU2222 Keywords Search [AC自动机模板]
- ac自动机模板hdu2222
- AC自动机-HDU2222-模板题
- HDU2222 Keywords Search [AC自动机模板]
- AC自动机 模板 【HDU2222】 Keywords Search
- AC自动机模板 hdu2222
- ac自动机模板(hdu2222)
- hdu2222 ac自动机模板
- AC自动机入门+模板 (HDU 2222)
- HDU2222 Keywords Search(AC自动机入门)
- ac自动机模板(hdu2222)
- HDU2222 Keywords Search(AC自动机模板)
- AC自动机入门——HDU2222
- 【HDU2222】【AC自动机模板 测烂为止】Keywords Search
- HDU2222 Keywords Search 【AC自动机模板题】
- HDU2222_Keywords Search _AC自动机模板题
- AC自动机入门题目(HDU - 2222 )+模板+解释
- AC自动机入门详解+例题 hdu2222