First of all, the AC automaton is not the accept automaton, do not think that the code copied to the OJ on the automatic AC ...
Actually this thing is Aho-corasick made, so you know. So what does this thing do? • String matching problem • Multi-string matching problem * Don't you understand? Explain: For example, give a few words Acbs,asf,dsef, and then give a very long article, acbsdfgeasf, ask in this article, how many words in total, or the total number of words appear. How to achieve it, is the Kmp+trie tree. is based on the KMP algorithm, trie as the index structure of the Dongdong. So how does it relate to KMP? • The key is to add a fail pointer to the trie tree. fail The purpose of the pointer: it is like an array of next in KMP. Determines the node to be transferred when a string mismatch. The AC difficulty is the pointer algorithm, see the following so many graphs: template:
1 structaho_corasick{2 intCH[MAXN][SIGC], MS;3 intVAL[MAXN], F[MAXN], LEN[MAXN];4 intLAST[MAXN];5 Aho_corasick () {}6 voidinit () {7ms =0;8Memset (Val,0,sizeof(Val));9memset (CH,0,sizeof(CH));TenMemset (F,0,sizeof(f)); OneMemset (Last,0,sizeof(last)); Amemset (Len,0,sizeof(len)); - return ; - } the voidInsertChar* S,intv) { - intCur =0, I; - for(i =0; S[i]! =' /'; i + +){ - intc = S[i]-'0'; + if(!ch[cur][c]) ch[cur][c] = + + ms;//1 -Cur =Ch[cur][c]; + } AVal[cur] = v; Len[cur] =i; at return ; - } - voidMake_fail () { -queue<int>Q; - for(inti =0; i < SIGC; i + +)if(ch[0][i]) Q.push (ch[0][i]); - while(!Q.empty ()) { in intx =Q.front (); Q.pop (); - for(intc =0; C <2; C + +){ to intv =Ch[x][c]; + if(!V) {Ch[x][c] = Ch[f[x]][c];Continue; } - Q.push (v); the intCur =F[x]; * while(cur &&!ch[cur][c]) cur =F[cur]; $F[V] =Ch[cur][c];Panax NotoginsengLAST[V] = Val[f[v]]?F[v]: last[f[v]; - } the } + return ; A } the};
Aho_corasick automatic Machine (AC automaton)