zoukankan      html  css  js  c++  java
  • hdu 2222 Keywords Search

    AC自动机裸题

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 using namespace std;
     5 const int maxnode=10000*50+5;
     6 char in[1000000+5];
     7 struct Trie
     8 {
     9     int ch[maxnode][26];
    10     int val[maxnode];
    11     int fail[maxnode];
    12     int sz;
    13     void initial(){sz=1;memset(ch[0],0,sizeof(ch[0]));memset(val,0,sizeof(val));}
    14     void insert(char *s)
    15     {
    16         int u=0,n=strlen(s);
    17         for(int i=0;i<n;i++)
    18         {
    19             int c=s[i]-'a';
    20             if(!ch[u][c])
    21             {
    22                 memset(ch[sz],0,sizeof(ch[sz]));
    23                 ch[u][c]=sz++;
    24             }
    25             u=ch[u][c];
    26         }
    27         val[u]++;
    28     }
    29     void GetFail()
    30     {
    31         queue<int> q;
    32         fail[0]=0;
    33         for(int c=0;c<26;c++)
    34         {
    35             int u=ch[0][c];
    36             if(u)
    37             {
    38                 fail[u]=0;q.push(u);
    39             }
    40         }
    41         while(!q.empty())
    42         {
    43             int r=q.front();q.pop();
    44             for(int c=0;c<26;c++)
    45             {
    46                 int u=ch[r][c];
    47                 if(!u) continue;
    48                 q.push(u);
    49                 int v=fail[r];
    50                 while(v&&!ch[v][c]) v=fail[v];
    51                 fail[u]=ch[v][c];
    52             }
    53         }
    54     }
    55     int solve(char *s)
    56     {
    57         int len=strlen(s);
    58         int cnt=0,u=0;
    59         for(int i=0;i<len;i++)
    60         {
    61             int c=s[i]-'a';
    62             while(u&&!ch[u][c]) u=fail[u];
    63             u=ch[u][c];
    64             int tmp=u;
    65             while(tmp&&val[tmp]!=-1)
    66             {
    67                 cnt+=val[tmp];
    68                 val[tmp]=-1;
    69                 tmp=fail[tmp];
    70             }
    71         }
    72         return cnt;
    73     }
    74 };
    75 Trie trie;
    76 int main()
    77 {
    78     int t,n;
    79     scanf("%d",&t);
    80     while(t--)
    81     {
    82         trie.initial();
    83         scanf("%d",&n);
    84         for(int i=0;i<n;i++)
    85         {
    86             scanf("%s",in);
    87             trie.insert(in);
    88         }
    89         trie.GetFail();
    90         scanf("%s",in);
    91         printf("%d
    ",trie.solve(in));
    92     }
    93     return 0;
    94 }
  • 相关阅读:
    js正则还原和转义特殊字符
    element表格鼠标悬浮上带有点击事件的变红-:row-class-name
    elemen-table表格数据转换-formatter属性
    SVN的安装及汉化的
    element中关于input框
    VUE之兄弟组件 $emit和$on、$bus的用法
    关于element表单校验(二)
    关于element表单校验(一)
    element表格里数据处理
    各类手册收藏整理
  • 原文地址:https://www.cnblogs.com/sooflow/p/3365751.html
Copyright © 2011-2022 走看看