See N<=20 billion immediately dumbfounded. Using matrix multiplication on AC automata to optimize DP ... Sxbk
Build an AC automaton, remove the illegal node and find out the Trie diagram ...
Then build a matrix based on the transfer relationship in the Trie diagram .... Finally run a fast power
Actually got out ... Touching
Brain various short circuits. First moment by the dozen hang, then is trie figure to seek measure tat. A whole evening of self-study was adjusted.
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 #definell Long Long6 using namespacestd;7 Const intModd=100000;8 intv[257],dl[103],fail[103],num[103];9 intch[103][4],tot,next[103][4];Tenll mp[103][103],cnt; Onell c[103][103],tmp[103][103],ans; A intI,j,k,n,m,l,r; - BOOLgg[103]; - Chars[ at]; the -InlinevoidTrieintN) { - inti,p=0; - for(i=0; i<n;i++){ + if(!ch[p][v[s[i]]) ch[p][v[s[i]]]=++tot,p=tot; - Elsep=Ch[p][v[s[i]]; + } Agg[p]=1;//printf ("GG:%d\n", p); at } -InlinevoidGetfail () { - intL=0, r=1, i,j,now,p;dl[1]=0; - while(l<R) { -NOW=DL[++L];//printf ("%d fail:%d gg:%d\n", Now,fail[now],gg[now]); - for(i=0;i<4; i++)if(Ch[now][i]) { inJ=ch[now][i];//printf ("%d-->%d\n", now,j); - for(P=fail[now];p &&!ch[p][i];p =fail[p]); to if(!now) fail[j]=0;Elsefail[j]=Ch[p][i]; +dl[++r]=j;gg[j]|=Gg[fail[j]]; - } the } * } $InlinevoidGetNext () {Panax NotoginsengL=0, r=1;inti,now,p;dl[1]=0; - BOOLflag=0; the while(l<R) { +NOW=DL[++L];//printf ("%d\n", now); A for(i=0;i<4; i++){ the if(Ch[now][i]) { + if(Gg[ch[now][i]]) next[now][i]=-1; - Elsenext[now][i]=ch[now][i],dl[++r]=Ch[now][i]; $ } $ Else{ - for(P=fail[now];p &&!ch[p][i];p =fail[p]); -next[now][i]=gg[ch[p][i]]?-1: Ch[p][i]; the } - //printf ("%d%d next:%d\n", Now,i,next[now][i]);Wuyi } the } - } WuInlinevoidupd () { -Cnt=0;inti,j; About for(i=1; i<=r;i++) $num[dl[i]]=++CNT; - for(i=1; i<=r;i++){ -j=Dl[i]; - for(k=0;k<4; k++)if(next[j][k]!=-1) Amp[num[next[j][k]]][num[j]]++; + } the - //for (I=1;i<=r;puts (""), i++) $ //for (j=1;j<=r;j++) printf ("%lld", Mp[i][j]); the } the the theInlinevoidMultoc () { -Registerinti,j,k; in for(i=1; i<=cnt;i++) the for(j=1; j<=cnt;tmp[i][j]%=modd,j++) the for(tmp[i][j]=0, k=1; k<=cnt;k++) tmp[i][j]+=mp[i][k]*C[k][j]; About for(i=1; i<=cnt;i++) memcpy (C[i],tmp[i], (cnt+1) <<3); the } theInlinevoidMultomp () { theRegisterinti,j,k; + for(i=1; i<=cnt;i++) - for(j=1; j<=cnt;tmp[i][j]%=modd,j++) the for(tmp[i][j]=0, k=1; k<=cnt;k++) tmp[i][j]+=mp[i][k]*Mp[k][j];Bayi for(i=1; i<=cnt;i++) memcpy (Mp[i],tmp[i], (cnt+1) <<3); the } the intMain () { -v['A']=0, v['C']=1, v['G']=2, v['T']=3; - thescanf"%d%d",&n,&m); the for(i=1; i<=n;i++) scanf ("%s", s), Trie (strlen (s)); the Getfail (), GetNext (), upd (); the for(i=1; i<=cnt;i++) c[i][i]=1; - the while(m) { the if(m&1) the Multoc ();94m>>=1;if(M) Multomp (); the } the for(i=1, ans=0; i<=cnt;i++) ans= (ans+c[i][1])%modd; theprintf"%lld\n", ans);98 return 0; About}View Code
[POJ2778] DNA Sequence