Test Instructions:Link slightly
Method:AC automata + matrix multiplication
parsing:and POJ 27,781-like questions. The general idea is that when we build an AC automaton, we need to be aware that if a point is the end of a string, then the nodes below are considered the end nodes. Then it is good to assign the internal values of the matrix according to the AC automata. The assigned matrix represents how many scenarios to walk from one node to another node. Then the classic model, the matrix of n-th square can.
Code:
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N#define Musing namespace STD;intN,m,k;intSize,root;intSON[N][M];intFail[n];intEnd[n];CharS[n];structmatrix{int MapN [N];} A,ans; Matrix &mul (Matrix &a,matrix &b) {StaticMatrix ret;memset(Ret.Map,0,sizeof(Ret.Map)); for(intI=1; i<=size;i++) { for(intj=1; j<=size;j++) { for(intk=1; k<=size;k++) {ret.MapI [J]+=a.MapI [K]*b.MapK [j]; } ret.MapI [J]%=k; } }returnRET;} Matrix &quick_power (Matrix &a,intb) {StaticMatrix ret;memset(Ret.Map,0,sizeof(Ret.Map)); for(intI=1; i<=size;i++) ret.MapI [i]=1; while(b) {if(b&1) Ret=mul (ret,a); A=mul (A,a); b>>=1; }returnRET;}voidInit () {memset(son,-1,sizeof(son)); Root=++size;}voidIns () {intlen=strlen(s+1);intNow=root; for(intI=1; i<=len;i++) {intalpha=s[i]-' 0 ';if(son[now][alpha]==-1) Son[now][alpha]=++size; Now=son[now][alpha]; } end[now]=1;}voidBuild () { Queue<int>Q for(intI=0; i<=9; i++) {if(son[root][i]==-1) Son[root][i]=root;Else{fail[son[root][i]]=root; Q.push (Son[root][i]); } } while(!q.empty ()) {intU=q.front (); Q.pop ();if(End[fail[u]]) end[u]=1; for(intI=0; i<=9; i++) {if(son[u][i]==-1) Son[u][i]=son[fail[u]][i];Else{Fail[son[u][i]]=son[fail[u]][i]; Q.push (Son[u][i]); } } }}voidBuild_matrix () { for(intI=1; i<=size;i++) {if(End[i])Continue; for(intj=0; j<=9; j + +) {if(End[son[i][j]])Continue; A.MapI [son[i][j]]++; } }}intMain () {init ();scanf("%d%d%d", &n,&m,&k);scanf('%s ', s+1); Ins (); Build (); Build_matrix (); Ans=quick_power (A,n);intPrint=0; for(intI=1; i<=size;i++) print= (Print+ans.Map[1][i])%K;printf("%d\n", print);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bzoj 1009 [hnoi2008]gt Test ac automata + matrix multiplication