Advertising:
#include <stdio.h>int main(){ puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44003109");}
Exercises
The process of establishing an AC automaton can be changed to KMP.
Anyway single string 233.
Code:
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define Tusing namespace STD;intN,m,p;structmrx{intX[T][T];} Ini,std,trs,e; MRXoperator* (ConstMRX &a,ConstMRX &b) {MRX c=e;intI,j,k; for(i=0; i<t;i++) for(j=0; j<t;j++) for(k=0; k<t;k++) (c.x[i][j]+= (Long Long) a.x[i][k]*b.x[k][j]%p)%=p;returnC;} MRX Power (MRX X,intp) {MRX ans=std; while(p) {if(p&1) ans=ans*x; x=x*x,p>>=1; }returnAns;}structacauto{intson[t][t],cnt;CharSRC[T];voidBuild_trie () {scanf('%s ', SRC);inti,x=0, ALP; for(i=0; src[i];i++) {alp=src[i]-' 0 ';if(!son[x][alp]) son[x][alp]=++cnt; X=son[x][alp]; } } Queue<int>QintFAIL[T];voidBuild_fail () { while(!q.empty ()) Q.pop (); Q.push (1);intI,u,v; while(!q.empty ()) {U=q.front (), Q.pop (); for(i=0;i<Ten; i++) {if(V=son[u][i]) {if(u) fail[v]=son[fail[u]][i];Elsefail[v]=1; Q.push (v); }ElseSon[u][i]=son[fail[u]][i]; } } }voidBuild_mrx () {intI,j,k; for(i=0; i<cnt;i++) for(j=0;j<Ten; j + +) trs.x[son[i][j]][i]++; }}acauto;intMain () {Freopen ("Test.in","R", stdin);scanf("%d%d%d", &n,&m,&p); Acauto.build_trie (); Acauto.build_fail (); Acauto.build_mrx (); for(intI=0; i<t;i++) std.x[i][i]=1; ini.x[0][0]=1; MRX Temp=power (trs,n); Temp=temp*ini;intans=0; for(intI=0; i<m;i++) ans= (ans+temp.x[i][0])%p;printf("%d\n", ans);/ * MRX Tot=ini; for (int i=1;i<=n;i++) tot=trs*tot;*/ return 0;}
"BZOJ1009" "HNOI2008" GT Test AC automata + matrix multiplication