Not a sound of dishes ac automatic machine brush title record

HDU2222 Keywords Search

  • Template title. The results will open a small array of T. . .
  • Code
     1 #include <bits/stdc++.h>
     2 #define nmax 10010
     3 
     4 using namespace std;
     5 char in[nmax],b[1000010];
     6 int t[50*nmax][30];
     7 int f[50*nmax],v[nmax*50];
     8 int n,cnt=0,ans=0;
     9 
    10 void ins(){
    11     int idx=0,l=strlen(in);
    12     for (int i=0; i<l; i++) {
    13         int x=in[i]-'a';
    14         if(!t[idx][x]) t[idx][x]=++cnt;
    15         idx=t[idx][x];
    16     }
    17     v[idx]++;
    18 }
    19 
    20 void init(){
    21     for (int i=0; i<=cnt; i++) {
    22             for (int j=0; j<26; j++) t[i][j]=0;
    23             f[i]=v[i]=0;
    24     }
    25     cnt=0;
    26     ans=0;
    27 }
    28 
    29 void bf(){
    30     queue <int> q;
    31     for (int i=0; i<26; i++) if(t[0][i]) q.push(t[0][i]);
    32     while(!q.empty()){
    33         int u=q.front();
    34         q.pop();
    35         for (int i=0; i<26; i++) {
    36             if( t[u][i] ) {
    37                 f[t[u][i]]=t[f[u]][i];
    38                 q.push(t[u][i]);
    39             }else t[u][i]=t[f[u]][i];
    40         }
    41     }
    42 }
    43 
    44 void solve(){
    45     int p=0,l=strlen(b);
    46     for (int i=0; i<l; i++) {
    47         int x=b[i]-'a';
    48         p=t[p][x];
    49         for (int j=p; j&&~v[j]; j=f[j]) ans+=v[j],v[j]=-1;
    50     }
    51 }
    52 
    53 int main(){
    54     int cas;
    55     cin>>cas;
    56     while(cas--){
    57         init();
    58         scanf("%d",&n);
    59         for (int i=0; i<n; i++) { scanf("%s",in); ins(); }
    60         bf();
    61         scanf("%s",b);
    62         solve();
    63         printf("%d\n",ans);
    64     }
    65     return 0;
    66 }
    o (* ¯ ▽ ¯ *) Breakfast

     

HDU2896 viruses

 

Guess you like

Origin www.cnblogs.com/jiecaoer/p/11365743.html
Recommended