AC自动机0

字符出现次数之和

预处理

  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <cmath>
  4 #include <cstring>
  5 #include <time.h>
  6 #include <string>
  7 #include <set>
  8 #include <map>
  9 #include <list>
 10 #include <stack>
 11 #include <queue>
 12 #include <vector>
 13 #include <bitset>
 14 #include <ext/rope>
 15 #include <algorithm>
 16 #include <iostream>
 17 using namespace std;
 18 #define ll long long
 19 #define minv 1e-6
 20 #define inf 1e9
 21 #define pi 3.1415926536
 22 #define E  2.7182818284
 23 const ll mod=1e9+7;//998244353
 24 const int maxn=1e6+10;
 25 
 26 struct node
 27 {
 28     int c,index;
 29     ll g;
 30     node* pre;
 31     node* next[26];
 32     node* fail;
 33 }*tr,*be,*q[maxn];
 34 
 35 char str[maxn];
 36 int tot[maxn];
 37 
 38 int main()
 39 {
 40     node *p,*d,*pos;
 41     int n,c,i,j,k,len,head,tail,num=0,sum=0;
 42     tr=(node*) malloc (sizeof(node));
 43     tr->g=0;
 44     for (i=0;i<26;i++)
 45         tr->next[i]=NULL;
 46     scanf("%d",&n);
 47     while (n--)
 48     {
 49         scanf("%s",str);
 50         len=strlen(str);
 51         pos=tr;
 52         for (j=0;j<len;j++)
 53         {
 54             c=str[j]-97;
 55             if (pos->next[c])
 56                 pos=pos->next[c];
 57             else
 58             {
 59                 p=(node*) malloc (sizeof(node));
 60                 for (k=0;k<26;k++)
 61                     p->next[k]=NULL;
 62                 p->c=c;
 63                 p->g=0;
 64                 num++;
 65                 p->index=num;
 66                 pos->next[c]=p;
 67                 p->pre=pos;
 68                 pos=p;
 69             }
 70         }
 71         pos->g++;
 72     }
 73     be=(node*) malloc (sizeof(node));
 74     for (i=0;i<26;i++)
 75         be->next[i]=tr;///all!
 76     tr->pre=be;
 77     tr->g=0;
 78 
 79     head=0;
 80     tail=1;
 81     q[1]=tr;
 82 
 83     while (head<tail)
 84     {
 85         head++;
 86         if (head==1)
 87             q[head]->fail=be;
 88         else
 89         {
 90             pos=q[head]->pre ->fail;
 91             c=q[head]->c;
 92             while (pos->next[c] == NULL)
 93                 pos=pos->fail;
 94             q[head]->fail=pos->next[c];
 95             //汇集该点及该点所有fail,failk (k=0,1,2,……)
 96             q[head]->g+=q[head]->fail->g;
 97         }
 98 
 99         d=q[head];
100         for (i=0;i<26;i++)
101             if (d->next[i])
102             {
103                 tail++;
104                 q[tail]=d->next[i];
105             }
106     }
107 
108     scanf("%s",str);
109     len=strlen(str);
110     pos=tr;
111     for (i=0;i<len;i++)
112     {
113         c=str[i]-97;
114         if (pos->next[c])
115             pos=pos->next[c];
116         else
117         {
118             while (pos->next[c]==NULL)
119                 pos=pos->fail;
120             pos=pos->next[c];
121         }
122             tot[pos->index]+=pos->g;
123 
124 //        d=pos;
125 //        while (d!=tr)
126 //        {
127 //            tot[d->index]+=d->g;
128 //            d=d->fail;
129 //        }
130     }
131     for (i=1;i<=num;i++)
132         sum=max(sum,tot[i]);
133     cout<<sum;
134     return 0;
135 }
136 /*
137 2
138 ab
139 ac
140 abc
141 
142 2
143 ababc
144 abc
145 ababc
146 
147 4
148 ab
149 abc
150 abab
151 ababc
152 ababc
153 
154 6
155 ab
156 abc
157 abc
158 abc
159 abab
160 ababc
161 ababc
162 */

猜你喜欢

转载自www.cnblogs.com/cmyg/p/9545332.html
今日推荐