AC automata.
F[I][J] is expressed as the number of scenarios in the text string that match the J node in the dictionary tree for length i.
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong Longll;4 Const intn=6005, mod=10007;5ll ans1,ans2,f[ the][n];6 Chars[ the];intcnt,n,m;7queue<int>Q;8 structnode9 {Ten ints,v[ -],f; One }t[n]; A voidbuild () - { - intN=strlen (s);intnow=0; the for(intI=0; i<n;++i) - { - if(t[now].v[s[i]-'A']) now=t[now].v[s[i]-'A']; - Else{ +t[now].v[s[i]-'A']=++cnt;now=CNT; - } + } At[now].s=1; at } - voidGetfail () - { - for(intI=0;i< -;++i) - { - if(t[0].v[i]) in { -t[t[0].v[i]].f=0; Q.push (t[0].v[i]); to } + } - while(!q.empty ()) the { * intx=Q.front (); Q.pop (); $ for(intI=0;i< -;++i)Panax Notoginseng { - if(T[x].v[i]) the { +t[t[x].v[i]].f=T[t[x].f].v[i];q.push (T[x].v[i]); A } the Elset[x].v[i]=T[t[x].f].v[i]; +t[t[x].v[i]].s|=T[t[t[x].v[i]].f].s; - } $ } $ } - voidWork () - { thef[0][0]=1; - for(intI=1; i<=m;++i)Wuyi for(intj=0; j<=cnt;++j) { the if(T[J].S)Continue; - for(intk=0;k< -;++k) WuF[i][t[j].v[k]]= (f[i][t[j].v[k]]+f[i-1][J])%MoD; - } Aboutans1=1; $ for(intI=0; i<=cnt;++i)if(!T[I].S) ans2= (Ans2+f[m][i])%MoD; - for(intI=1; i<=m;++i) ans1=ans1* -%MoD; -printf"%lld\n", (ans1-ans2+mod)%MoD); - return; A } + intMain () the { -scanf"%d%d",&n,&m); $ for(intI=1; i<=n;++i) thescanf"%s", s), build (); the Getfail (); the Work (); the return 0; -}
BZOJ1030 JSOI2007 Text Generator