DNA Sequence
Description
It's well known that DNA Sequence are a Sequence only contains a, C, T and G, and it's very useful to analyze a segment of DNA Sequence,for example, if a animal ' s DNA Sequence contains segment ATC then it could mean that the animal could have a gene Tic disease. Until now scientists has found several those segments, the problem is how many kinds of DNA sequences of a species don ' t contain those segments.
Suppose that DNA sequences of a species are a sequence that consist of a, C, T and g,and the length of sequences are a given Integer n.
Input
First line contains (0 <= m <=), N (1 <= n <=2000000000). Here, M are the number of genetic disease segment, and n is the length of sequences.
Next m lines each line contain a DNA genetic disease segment, and length of these segments are not larger than 10.
Output
An integer, the number of DNA sequences, mod 100000.
Sample Input
4 3ATACAGAA
Sample Output
36
The idea is this: put all the virus fragments into the AC automaton and create a fail array. If the fail for a state is a virus node, then he is also the virus node himself. Finally, the matrix is built by edge, and the power is fast.
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <queue>5 using namespacestd;6 Const intmaxn= the;7 Const intMod=100000;8typedef unsignedLong Longull;9 structmatrix{Ten intN; One ull MAT[MAXN][MAXN]; AMatrix (intN_,inton=0){ -N=n_;memset (Mat,0,sizeof(MAT)); - if(ON) for(intI=1; i<=n;i++) mat[i][i]=1; the } -Matrixoperator*(Matrix a) { - Matrix ret (n); -UnsignedLong Longl; + for(intI=1; i<=n;i++) - for(intk=1; k<=n;k++){ +L=Mat[i][k]; A for(intj=1; j<=n;j++) at(Ret.mat[i][j]+=l*a.mat[k][j]%mod)%=MoD; - } - returnret; - } -Matrixoperator^(Long Longk) { -Matrix ret (n,1); in while(k) { - if(k&1) toret=ret** This; +k>>=1; -* This=* This** This; the } * returnret; $ }Panax Notoginseng }; - the structac_automation{ + BOOLTAG[MAXN]; A intcnt,rt,ch[maxn][4],FAIL[MAXN]; the ac_automation () { +memset (Tag,0,sizeof(tag)); -memset (fail,0,sizeof(fail)); $memset (CH,0,sizeof(CH)); cnt=rt=1; $ } - - intID (Charc) { the if(c=='A')return 0; - Else if(c=='C')return 1;Wuyi Else if(c=='G')return 2; the Else return 3; - } Wu - voidInsert (Char*R) { About intLen=strlen (s), p=RT; $ for(intI=0; i<len;i++) - if(Ch[p][id (S[i])) -p=Ch[p][id (S[i]); - Else AP=ch[p][id (S[i])]=++CNT; +tag[p]=true; the } - $ voidBuild () { thequeue<int>Q; the for(intI=0;i<4; i++) the if(Ch[rt][i]) thefail[ch[rt][i]]=Rt,q.push (Ch[rt][i]); - Else inch[rt][i]=RT; the the while(!Q.empty ()) { About intx=Q.front (); Q.pop (); the for(intI=0;i<4; i++) the if(Ch[x][i]) { thefail[ch[x][i]]=Ch[fail[x]][i]; +tag[ch[x][i]]|=Tag[fail[ch[x][i]]; - Q.push (Ch[x][i]); the }Bayi Else thech[x][i]=Ch[fail[x]][i]; the } - } - the voidSolve (intk) { the Matrix A (CNT); the for(intI=1; i<=cnt;i++) the for(intj=0;j<4; j + +) - if(!tag[i]&&!Tag[ch[i][j]]) thea.mat[ch[i][j]][i]+=1; thea=a^K; the Long Longans=0;94 for(intI=1; i<=cnt;i++) theans+=a.mat[i][1]; theprintf"%lld\n", ans%MoD); the }98 }ac; About CharS[MAXN]; - 101 intMain () {102 #ifndef Online_judge103 //freopen ("", "R", stdin);104 //freopen ("", "w", stdout); the #endif106 intTot,n;107scanf"%d%d",&tot,&n);108 while(tot--){109scanf"%s", s); the AC. Insert (s);111 } the AC. Build ();113 AC. Solve (n); the return 0; the}
Linear algebra (Matrix multiplication): POJ 2778 DNA Sequence