AC自动机 hdu 2222 Keywords Search

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222

  今天木有题意,也木有分析,AC自动机裸题,省赛前敲一敲(抄一抄)代码。

AC代码:

  1 #include<bits/stdc++.h>
  2 
  3 using namespace std;
  4 
  5 struct Aho{
  6     struct state{
  7         int next[26];
  8         int fail,cnt;
  9     }StateTable[500005];
 10     int size;
 11     queue<int>q;
 12     void init(){
 13         while(!q.empty()) q.pop();
 14         for(int i=0;i<500005;i++){
 15             memset(StateTable[i].next,0,sizeof(StateTable[i].next));
 16             StateTable[i].fail=StateTable[i].cnt=0;
 17         }
 18         size=0;
 19     }
 20     void insert(char *s){
 21         int n=strlen(s);
 22         int now=0;
 23         for(int i=0;i<n;i++){
 24             char c=s[i];
 25             if(!StateTable[now].next[c-'a']){
 26                 StateTable[now].next[c-'a']=++size;
 27             }
 28             now=StateTable[now].next[c-'a'];
 29         }
 30         StateTable[now].cnt++;
 31     }
 32     void build(){
 33         StateTable[0].fail=-1;
 34         q.push(0);
 35         while(!q.empty()){
 36             int u=q.front();
 37             q.pop();
 38             for(int i=0;i<26;i++){
 39                 if(StateTable[u].next[i]){
 40                     if(u==0){
 41                         StateTable[StateTable[u].next[i]].fail=0;
 42                     }
 43                     else {
 44                         int v=StateTable[u].fail;
 45                         while(v!=-1){
 46                             if(StateTable[v].next[i]){
 47                                 StateTable[StateTable[u].next[i]].fail=StateTable[v].next[i];
 48                                 break;
 49                             }
 50                             v=StateTable[v].fail;
 51                         }
 52                         if(v==-1){
 53                             StateTable[StateTable[u].next[i]].fail=0;
 54                         }
 55                     }
 56                     q.push(StateTable[u].next[i]);
 57                 }
 58             }
 59         }
 60     }
 61 
 62     int get(int u){
 63         int res=0;
 64         while(u){
 65             res+=StateTable[u].cnt;
 66             StateTable[u].cnt=0;
 67             u=StateTable[u].fail;
 68         }
 69         return res;
 70     }
 71     int macth(char *s){
 72         int n=strlen(s);
 73         int res=0,now=0;
 74         for(int i=0;i<n;i++){
 75             char c=s[i];
 76             if(StateTable[now].next[c-'a']){
 77                 now=StateTable[now].next[c-'a'];
 78             }
 79             else {
 80                 int p=StateTable[now].fail;
 81                 while(p!=-1&&StateTable[p].next[c-'a']==0){
 82                     p=StateTable[p].fail;
 83                 }
 84                 if(p==-1){
 85                     now=0;
 86                 }
 87                 else {
 88                     now=StateTable[p].next[c-'a'];
 89                 }
 90             }
 91             if(StateTable[now].cnt){
 92                 res+=get(now);
 93             }
 94         }
 95       return res;
 96     }
 97 }aho;
 98 int t,n;
 99 char s[1000006];
100 int main(){
101     ios_base::sync_with_stdio(false);
102     cin.tie(0);
103     cin>>t;
104     while(t--){
105         aho.init();
106         cin>>n;
107         for(int i=1;i<=n;i++){
108             cin>>s;
109             aho.insert(s);
110         }
111         aho.build();
112         cin>>s;
113         cout<<aho.macth(s)<<endl;
114     }
115     return 0;
116 }
View Code

猜你喜欢

转载自www.cnblogs.com/ls961006/p/8987673.html