标签:
首先,AC自动机不是Accept自动机,别以为把这段代码复制到OJ上就全都自动AC了……
1 struct Aho_Corasick{ 2 int ch[maxn][Sigc], ms; 3 int val[maxn], f[maxn], len[maxn]; 4 int last[maxn]; 5 Aho_Corasick(){} 6 void init(){ 7 ms = 0; 8 memset(val, 0, sizeof(val)); 9 memset(ch, 0, sizeof(ch)); 10 memset(f, 0, sizeof(f)); 11 memset(last, 0, sizeof(last)); 12 memset(len, 0, sizeof(len)); 13 return ; 14 } 15 void insert(char* s, int v){ 16 int cur = 0, i; 17 for(i = 0; s[i] != ‘\0‘; i ++){ 18 int c = s[i] - ‘0‘; 19 if(!ch[cur][c]) ch[cur][c] = ++ ms; // 1 20 cur = ch[cur][c]; 21 } 22 val[cur] = v; len[cur] = i; 23 return ; 24 } 25 void make_fail(){ 26 queue<int> Q; 27 for(int i = 0; i < Sigc; i ++) if(ch[0][i]) Q.push(ch[0][i]); 28 while(!Q.empty()){ 29 int x = Q.front(); Q.pop(); 30 for(int c = 0; c < 2; c ++){ 31 int v = ch[x][c]; 32 if(!v) { ch[x][c] = ch[f[x]][c]; continue; } 33 Q.push(v); 34 int cur = f[x]; 35 while(cur && !ch[cur][c]) cur = f[cur]; 36 f[v] = ch[cur][c]; 37 last[v] = val[f[v]] ? f[v] : last[f[v]]; 38 } 39 } 40 return ; 41 } 42 };
标签:
原文地址:http://www.cnblogs.com/chxer/p/4390976.html