Topic links
AC Automatic Machine +DP
A bit like a GT test, Hh[i][j] represents the number of scenarios that match to the J-number node on an automaton
1#include <algorithm>2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <string>7#include <cmath>8#include <ctime>9#include <queue>Ten#include <stack> One#include <map> A#include <Set> - #defineRre (i,r,l) for (int i= (r); i>= (l); i--) - #defineRe (I,L,R) for (int i= (l); i<= (R); i++) the #defineClear (A, B) memset (A,b,sizeof (a)) - #defineInOut (x) printf ("%d", (x)) - #defineDouin (x) scanf ("%lf", &x) - #defineStrin (x) scanf ("%s", (x)) + #defineLLin (x) scanf ("%lld", &x) - #defineOP operator + #defineCSC Main Atypedef unsignedLong LongULL; attypedefConst intCInt; -typedefLong LongLL; - using namespacestd; - voidInin (int&ret) - { -ret=0;intf=0;CharCh=GetChar (); in while(ch<'0'|| Ch>'9'){if(ch=='-') f=1; ch=GetChar ();} - while(ch>='0'&&ch<='9') ret*=Ten, ret+=ch-'0', ch=GetChar (); toret=f?-Ret:ret; + } - intn,m,ch[6060][ -],pre[6060],hh[111][6060],v[6060],ed=1, last[6060]; the Chars[1010]; * voidAddChar*s,intx) $ {Panax Notoginseng inttemp=1, len=strlen (s); -Re (I,0, len-1) the { + intc=s[i]-'A'; A if(!ch[temp][c]) ch[temp][c]=++Ed; thetemp=Ch[temp][c]; + } -v[temp]=x; $ } $ voidGetfail () - { -queue<int>h;intK; theH.push (1); - while(!h.empty ())Wuyi { the intx=H.front (); H.pop (); -Re (I,0, -) Wu { - if(!ch[x][i])Continue; About intvv=Ch[x][i]; $ H.push (vv); -k=Pre[x]; - while(!ch[k][i]) k=Pre[k]; -pre[vv]=Ch[k][i]; A if(V[ch[k][i]]) v[ch[x][i]]=V[ch[k][i]]; +LAST[VV]=V[PRE[VV]]?PRE[VV]:LAST[PRE[VV]]; the } - } $ } theCInt mod=10007; the voiddpintx) the { theRe (I,1, ed) - { in if(v[i]| |! hh[x-1][i])Continue; theRe (J,0, -) the { About intk=i; the while(!ch[k][j]) k=Pre[k]; the(hh[x][ch[k][j]]+=hh[x-1][i])%=MoD; the } + } - } the intMain ()Bayi { the inin (n), Inin (m); theRe (I,0, -) ch[0][i]=1; -Re (I,1, N) - { the Strin (s); the Add (s,i); the } the Getfail (); -Ght0][1]=1; theRe (I,1, M) DP (i); the intans1=0, ans2=1; theRe (I,1, m) ans2=ans2* -%MoD;94Re (I,1, ed)if(!v[i]) (Ans1+=hh[m][i])%=MoD; theprintf"%d", (ans2-ans1+mod)%MoD); the return 0; the}
bzoj1030 [JSOI2007] Text generator