AC automaton nude problem, really nude ...
The first n to match the string to save, and then the M-mode string structure of the AC automata, and then asked to be matched to the string is not ...
1#include <stdio.h>2#include <string.h>3#include <queue>4 using namespacestd;5 Const intmaxm=600006;6 7 Chars[100005][10005],word[10005];8 intnxt[maxm][ -],tail[maxm],f[maxm],size;9 intLAST[MAXM];Ten One intNewNode () { Amemset (Nxt[size],0,sizeof(Nxt[size])); -f[size]=tail[size]=0; - returnsize++; the } - - voidInsertChars[]) { - inti,p=0; + for(i=0; s[i];i++){ - int&x=nxt[p][s[i]-'a']; +p=x?x:x=NewNode (); A } attail[p]++; - } - - voidmakenxt () { - inti; -queue<int>Q; inf[0]=0; - for(i=0;i< -; i++){ to intv=nxt[0][i]; + if(v) { -f[v]=last[v]=0; the Q.push (v); * } $ }Panax Notoginseng while(!Q.empty ()) { - intu=Q.front (); the Q.pop (); + for(i=0;i< -; i++){ A intv=Nxt[u][i]; the if(!V) nxt[u][i]=Nxt[f[u]][i]; + Else{ - Q.push (v); $f[v]=Nxt[f[u]][i]; $LAST[V]=TAIL[F[V]]?F[v]:last[f[v]]; - } - } the } - }Wuyi the intQueryChars[]) { - intans=0, v=0; Wu for(intI=0; s[i];i++){ - while(v&&!nxt[v][s[i]-'a']) v=F[v]; Aboutv=nxt[v][s[i]-'a']; $ inttmp=v; - while(TMP) { -ans+=tail[tmp]; -tmp=last[tmp]; A } + } the returnans; - } $ the intMain () { the intT; thescanf"%d",&T); the while(t--){ - intn,m; inscanf"%d%d",&n,&m); theSize=0, NewNode (); the for(intI=0; i<n;++i) { Aboutscanf"%s", S[i]); the } the for(intI=0; i<m;++i) { thescanf"%s", word); + Insert (word); - } the makenxt ();Bayi for(intI=0; i<n;++i) { theprintf"%d\n", Query (S[i])); the } - } - return 0; the}
View Code
HDU 5384 AC automatic machine