HDU 2222 Keywords Search

没啥可说的,AC自动机模板。模仿lrj和NotOnlySuccess大大写的

推荐教程o(╯□╰)o http://www.cs.uku.fi/~kilpelai/BSA05/lectures/slides04.pdf

 

  1 #include <cstdio>
  2 #include <sstream>
  3 #include <fstream>
  4 #include <cstring>
  5 #include <iostream>
  6 #include <algorithm>
  7 #include <map>
  8 #include <cctype>
  9 #include <ctime>
 10 #include <set>
 11 #include <climits>
 12 #include <vector>
 13 #include <queue>
 14 #include <stack>
 15 #include <cstdlib>
 16 #include <cmath>
 17 #include <string>
 18 #include <list>
 19 
 20 #define INPUT_FILE "in.txt"
 21 #define OUTPUT_FILE "out.txt"
 22 
 23 using namespace std;
 24 
 25 typedef long long LL;
 26 const int INF = INT_MAX / 2;
 27 
 28 void setfile() {
 29     freopen(INPUT_FILE,"r",stdin);
 30     freopen(OUTPUT_FILE,"w",stdout);
 31 }
 32 
 33 const int maxn = 10000 + 5;
 34 const int maxlen = 51;
 35 const int maxtlen = 1000000 + 5;
 36 const int maxnode = maxn * maxlen;
 37 const int sigma_size = 26;
 38 
 39 struct AC_automation {
 40     int sz;
 41     int ch[maxnode][sigma_size];
 42     int val[maxnode];
 43     int fail[maxnode];
 44 
 45     void init() {
 46         memset(ch[0],0,sizeof(ch[0]));
 47         fail[0] = val[0] = 0;
 48         sz = 1;
 49     }
 50 
 51     inline int idx(char c) {
 52         return c - 'a';
 53     }
 54 
 55     void insert(char *str) {
 56         int len = strlen(str),u = 0;
 57         for(int i = 0;i < len;i++) {
 58             int c = idx(str[i]);
 59             if(ch[u][c] == 0) {
 60                 memset(ch[sz],0,sizeof(ch[sz]));
 61                 fail[sz] = val[sz] = 0;
 62                 ch[u][c] = sz++;
 63             }
 64             u = ch[u][c];
 65         }
 66         val[u]++;
 67     }
 68 
 69     void construct() {
 70         queue<int> q;
 71         int u = 0;
 72         for(int i = 0;i < sigma_size;i++) if(ch[u][i]) {
 73             fail[ ch[u][i] ] = 0;
 74             q.push(ch[u][i]);
 75         }
 76         while(!q.empty()) {
 77             u = q.front(); q.pop();
 78             for(int i = 0;i < sigma_size;i++) {
 79                 int &v = ch[u][i];
 80                 if(v) {
 81                     q.push(v);
 82                     fail[v] = ch[ fail[u] ][i];
 83                 } else {
 84                     v = ch[ fail[u] ][i];
 85                 }
 86             }
 87         }
 88     }
 89 
 90     int query(char *str) {
 91         int len = strlen(str),u = 0;
 92         int ans = 0;
 93         for(int i = 0;i < len;i++) {
 94             int c = idx(str[i]);
 95             u = ch[u][c];
 96             int v = u;
 97             while(v) {
 98                 ans += val[v];
 99                 val[v] = 0;
100                 v = fail[v];
101             }
102         }
103         return ans;
104     }
105 };
106 
107 char word[maxlen],T[maxtlen];
108 AC_automation ac;
109 
110 int main() {
111     int kase; scanf("%d",&kase);
112     while(kase--) {
113         ac.init();
114         int n; scanf("%d",&n);
115         for(int i = 0;i < n;i++) {
116             scanf("%s",word);
117             ac.insert(word);
118         }
119         ac.construct();
120         scanf("%s",T);
121         printf("%d\n",ac.query(T));
122     }
123     return 0;
124 }

 

posted @ 2014-04-10 11:33  acm_roll  阅读(152)  评论(0编辑  收藏  举报