http://acm.hdu.edu.cn/showproblem.php?pid=2222
KMP is a single-mode string matching algorithm, and AC automata is an algorithm for multi-mode string matching. Mainly composed of trie and KMP's thoughts.
Test instructions: Enter n pattern string, and then give a text string, find the number of pattern strings appearing in the text string.
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <cstdlib>5#include <algorithm>6#include <string>7#include <iostream>8#include <stack>9#include <map>Ten#include <queue> One using namespacestd; A #defineN 500001 - /* - when a character match of a pattern string fails, it jumps to its failed pointer to continue the match, repeats the above operation until the character matches successfully, so the failed pointer must satisfy a property, it must point to a string prefix, and this prefix is the prefix of the current node suffix, and must be the longest suffix. the */ - structAc_dfa - { - intnext[n][ -];//It started with a char result, mle. + intVal[n], size, root, fail[n]; - + intCreat () {//construct a new node A for(inti =0; I < -; i++) { atNext[size][i] =-1; - } -Val[size] =0; - returnsize++; - } - in voidinit () { -Size =0; toRoot =creat (); + } - the voidInsertCharS[]) {//Insert Pattern String * intLen =strlen (s); $ intnow =Root;Panax Notoginseng for(inti =0; i < Len; i++) { - intc = S[i]-'a'; the if(Next[now][c] = =-1) { +NEXT[NOW][C] =creat (); A } thenow =Next[now][c]; + } -val[now]++; $ } $ - voidBuild () {//construct the Fail function -queue<int>que; the while(!que.empty ()) Que.pop (); - for(inti =0; I < -; i++) {//InitializeWuyi if(Next[root][i] = =-1) {//if there's no edge, fill it up . theNext[root][i] =Root; -}Else { WuFail[next[root][i]] = root;//If you have a side, the first node points to root . - Que.push (Next[root][i]); About } $ } - while(!Que.empty ()) { - intnow =Que.front (); Que.pop (); - for(inti =0; I < -; i++) { A if(Next[now][i] = =-1) { +Next[now][i] = Next[fail[now]][i];//If there is no edge to construct an edge out the //The constructed edge is the out edge of the node to which the fail pointer points -}Else { $Fail[next[now][i]] = next[fail[now]][i];//A side fail points to an edge that is the same as the current node (prefixed with the longest suffix of the currently matched string) the Que.push (Next[now][i]); the } the } the } - } in the intQueryChars[]) { the intLen =strlen (s); About intAns =0; the intnow =Root; the for(inti =0; i < Len; i++) { thenow = Next[now][s[i]-'a']; + intTMP =Now ; - while(TMP! =root) { theAns + =val[tmp];BayiVAL[TMP] =0; theTMP = fail[tmp];//KMP thought: The current match fails, along with the mismatch edge to see if there is a matching string the } - } - returnans; the } the }; the the Chars[1000001]; - Ac_dfa Ac; the the intMain () the {94 intT; thescanf"%d", &t); the while(t--) { the intN;98scanf"%d", &n); About ac.init (); - for(inti =0; I < n; i++) {101scanf"%s", s);102 Ac.insert (s);103 }104 ac.build (); thescanf"%s", s);106 intAns =Ac.query (s);107printf"%d\n", ans);108 }109 return 0; the}
HDU 2222:keywords Search (ac automaton template)