zoukankan      html  css  js  c++  java
  • [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]

    AC自动机模板题(膜jcvb代码)

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstdio>
     4 #include <cstdlib>
     5 #include <cstring>
     6 #include <cmath>
     7 #include <ctime>
     8 #include <queue>
     9 
    10 using namespace std;
    11 
    12 char    s[110000],tt[110000],Ans[110000];
    13 int    ch[110000][26],Fail[110000],lab[110000],cnt,nv[110000];
    14 
    15 void    Add(const char * str)
    16 {
    17     int    p=0,i;
    18     for(i=1;str[i];++i)
    19     {
    20         if(!ch[p][str[i]-97])ch[p][str[i]-97]=++cnt;
    21         p=ch[p][str[i]-97];
    22     }
    23     lab[p]=i-1;
    24     return ;
    25 }
    26 
    27 void    Build()
    28 {
    29     int    i,t;
    30 
    31     queue<int>    Q;
    32     Q.push(0);
    33     while(!Q.empty())
    34     {
    35         t=Q.front();Q.pop();
    36         for(i=0;i<26;++i)
    37         {
    38             if(ch[t][i])
    39             {
    40                 Q.push(ch[t][i]);
    41                 Fail[ch[t][i]]=t?ch[Fail[t]][i]:0;
    42             }
    43             else    ch[t][i]=ch[Fail[t]][i];
    44         }
    45     }
    46     return ;
    47 }
    48 
    49 int main()
    50 {
    51     int    i,q,p,top=0;
    52 
    53     scanf("%s",s+1);
    54     scanf("%d",&q);
    55     for(i=1;i<=q;++i)
    56     {
    57         scanf("%s",tt+1);
    58         Add(tt);
    59     }
    60 
    61     Build();
    62     p=0;
    63     for(i=1;s[i];++i)
    64     {
    65         p=ch[p][s[i]-97];
    66         ++top;
    67         Ans[top]=s[i];
    68         nv[top]=p;
    69         if(lab[p])top-=lab[p],p=nv[top];
    70     }
    71 
    72     for(i=1;i<=top;++i)printf("%c",Ans[i]);
    73 
    74     printf("
    ");
    75     return 0;
    76 }
    View Code
  • 相关阅读:
    背水一战 Windows 10 (61)
    背水一战 Windows 10 (60)
    背水一战 Windows 10 (59)
    背水一战 Windows 10 (58)
    背水一战 Windows 10 (57)
    背水一战 Windows 10 (56)
    背水一战 Windows 10 (55)
    背水一战 Windows 10 (54)
    背水一战 Windows 10 (53)
    背水一战 Windows 10 (52)
  • 原文地址:https://www.cnblogs.com/Gster/p/4978013.html
Copyright © 2011-2022 走看看