POJ 2778 DNA Sequence ac automata + matrix fast Power

Source: Internet
Author: User

Links: http://poj.org/problem?id=2778

Test instructions: Given no more than 10 strings, each string of not more than 10 of the disaster gene; Q. How many genes do not contain a catastrophic gene in the length of a given length of not more than 2e9?

DNA: Contains only ' A ', ' T ', ' C ', ' G ' four characters;

Idea: This is not very nude AC automata. There is no obvious text string matching process, but we can pass the trie of the disaster gene, we need to initialize the state transition matrix when we run the mismatch edge.

State matrix: Each time you can go down a position to walk four directions, but the requirements can not go to the word node.

(Mat[i][j]) ^n: Said Walk n step, I means to start from that node, J represents the final character (not the character of characters, can represent more than one legal character);

$ ans = \sum \limits _{i=0}^{\rm size} mat\left [0 \right]\left [i \right]$

#include <iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<Set>#include<map>#include<queue>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))#defineMSi (a) memset (A,0x3f,sizeof (a))#defineINF 0x3f3f3f3f#defineLson L, M, RT << 1#defineRson m+1, R, RT << 1|1#defineSqr (a) (a) * (a)typedef pair<int,int>PII;#defineA First#defineB Second#defineMK Make_pairtypedef __int64 Ll;template<typename t>voidRead1 (T &L) {T x=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} M= x*F;} Template<typename t>voidRead2 (T &a,t &b) {Read1 (a); Read1 (b);} Template<typename t>voidRead3 (T &a,t &b,t &c) {Read1 (a); Read1 (b); Read1 (c);} Template<typename t>void  out(T a) {if(a>9) out(ATen); Putchar (A%Ten+'0');}intT,kase =0, I,j,k,n,m,top;#defineMoD 100000structmatrix{intd[107][107]; Matrix (intR =0) {MS0 (d); if(r) { for(inti =0; I < top;i++) D[i][i]=1; }} Matrixoperator*(Constmatrix& a)Const{Matrix ans;  for(inti =0; I < top;i++)         for(intj =0; J < top;j++){             for(intK =0; k < top;k++) Ans.d[i][j]= (Ans.d[i][j]+1ll*d[i][k]*a.d[k][j])%MoD; }        returnans; }}; Matrix Pow (Matrix a,ll m) {Matrix ans (1);  while(m) {if(m&1) ans = ans*A; A= A *A; M>>=1; }    returnans;}inth[ -];Const intSigma_size =4;Const intMAXN =107;structaho_corasick{intCh[maxn][sigma_size]; intVAL[MAXN],F[MAXN],LAST[MAXN],CNT[MAXN]; intsz; Map<string,int>MS; Aho_corasick () {}voidInit () {sz =1; val[0] =0; MS0 (ch[0]);    MS0 (CNT); Ms.clear ();} intIdxCharc) {returnh[c];} voidInsert (Char*s,intv) {        intU =0, n =strlen (s);  for(inti =0; I < n;i++){            intc =idx (s[i]); if(!Ch[u][c])                {MS0 (Ch[sz]); VAL[SZ]=0; CH[U][C]= sz++; } u=Ch[u][c]; } Val[u]=v; }    voidGetfail () {Queue<int>Q; f[0] =0; //Initialize Queue         for(intc =0; c < sigma_size;c++){            intU = ch[0][c]; if(u) {f[u] =0; Q.push (U); Last[u] =0;} }         while(!Q.empty ()) {            intR =Q.front (); Q.pop ();  for(intc =0; c < sigma_size;c++){                intU =Ch[r][c]; if(!u) {Ch[r][c] = Ch[f[r]][c];Continue;}//Implementing Compressionq.push (U); intv =F[r];  while(v &&!ch[v][c]) v =F[v]; F[u]=Ch[v][c]; Last[u]= Val[f[u]]?F[u]:last[f[u]]; }        }    }    //find the template from the text string;Matrix Find () {matrix ans;  for(inti =0; I < sz;i++){            if(Val[i] | | last[i])Continue;//Legal can for(intj =0; J <4; j + +) {//each node can generate 4 nodesintv =Ch[i][j]; if(Val[v] | | last[v])Continue; ANS.D[I][V]++;            ///can also indicate that there are several ways of representing the node I to the node V; }        }        returnans; }}ac;Charp[ the];intMain () {ll n,m; //freopen ("Data.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);h['A'] =0; h['C'] =1; h['T'] =2; h['G'] =3;    Ac.init ();    Read2 (N,M); REP1 (i,1, N) {scanf ("%s", p); Ac.    Insert (P,i); } Top=Ac.sz;    Ac.getfail (); Matrix ans=AC.    Find (); Ans=Pow (ans,m); intCNT =0; Rep0 (i,0, top) CNT+ = ans.d[0][i]; printf ("%d\n", cnt%MoD); return 0;}

POJ 2778 DNA Sequence ac automata + matrix fast Power

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.