标签:des style blog http color io os ar java
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6721 Accepted Submission(s): 2372
1 /*hdu 3065 Ac-自动机*/ 2 //#define LOCAL 3 #include<queue> 4 #include<cstdio> 5 #include<cstring> 6 #include<cstdlib> 7 #include<iostream> 8 #include<algorithm> 9 using namespace std; 10 int res; 11 struct Trie 12 { 13 struct Trie *fail; 14 struct Trie *child[127]; 15 int id; 16 }; 17 char s1[1001][55]; 18 char t1[2000005]; 19 int ans[1002]; 20 void _insert(char *s,Trie *root,int id) //构造一下rie树 21 { 22 Trie *cur=root,*newcur; 23 for(int i=0;s[i];i++) 24 { 25 int idx=s[i]-32; 26 if(cur->child[idx]==NULL) 27 { 28 newcur=new Trie; 29 for(int j=0;j<127;j++) 30 newcur->child[j]=NULL; 31 newcur->fail=NULL; 32 newcur->id=0; 33 cur->child[idx]=newcur; 34 } 35 cur=cur->child[idx]; 36 } 37 cur->id=id; 38 } 39 //构造失败指针 40 void ac_fail(Trie *root) 41 { 42 Trie *cur,*q ; 43 queue<Trie*>tre; 44 tre.push(root); 45 while(!tre.empty()) 46 { 47 cur=tre.front(); 48 tre.pop(); 49 for(int i=0;i<127;i++) 50 { 51 if(cur->child[i]) 52 { 53 if(cur==root) 54 cur->child[i]->fail=root; 55 else 56 { 57 q=cur; 58 while(q->fail){ 59 if(q->fail->child[i]) 60 { 61 cur->child[i]->fail=q->fail->child[i]; 62 break; 63 } 64 q=q->fail; 65 } 66 if(!q->fail) 67 cur->child[i]->fail=root; 68 } 69 tre.push(cur->child[i]); 70 } 71 } 72 } 73 } 74 //查询 75 void solve(char *s,Trie *root) 76 { 77 Trie *cur=root,*newcur; 78 for(int i=0 ; s[i] ;i++ ) 79 { 80 while(cur->child[s[i]-32]==NULL&&cur!=root) 81 cur=cur->fail; 82 cur=cur->child[s[i]-32]; 83 if(cur==NULL) cur=root; 84 newcur=cur; 85 while(newcur!=root&&newcur->id>0) 86 { 87 ans[newcur->id]++; 88 newcur=newcur->fail; 89 } 90 } 91 } 92 void del(Trie *root) 93 { 94 if(!root) return ; 95 for(int i=0;i<127;i++) 96 if(root->child[i]) 97 del(root->child[i]); 98 delete root; 99 } 100 void work(int n) 101 { 102 for(int i=1;i<=n;i++) 103 if(ans[i]>0) 104 { 105 printf("%s: %d\n",s1[i],ans[i]); 106 } 107 } 108 int main() 109 { 110 #ifdef LOCAL 111 freopen("test.in","r",stdin); 112 #endif 113 int n,i; 114 while(scanf("%d",&n)!=EOF) 115 { 116 Trie *root=new Trie; 117 for(i=0;i<127;i++) 118 root->child[i]=NULL; 119 root->fail=NULL; 120 root->id=0; 121 memset(ans,0,sizeof(ans)); 122 for(i=1;i<=n;i++) 123 { 124 scanf("%s",s1[i]); 125 _insert(s1[i],root,i); 126 } 127 ac_fail(root); 128 scanf("%s",t1); 129 solve(t1,root); 130 work(n); 131 del(root); 132 } 133 return 0; 134 }
标签:des style blog http color io os ar java
原文地址:http://www.cnblogs.com/gongxijun/p/4018255.html