POJ 2778 DNA Sequence (ac automata, matrix multiplication)

Source: Internet
Author: User

Test instructions: Given n a pattern string that cannot occur, given a length of M, the number of valid strings of length m is required.

Idea: Using AC automata, using the nodes on AC automata to do matrix multiplication.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <string>6#include <algorithm>7#include <queue>8 #defineMOD 1000009 Chars[2005];Ten intChCharx) { One     if(x=='A')return 0; A     if(x=='C')return 1; -     if(x=='G')return 2; -     if(x=='T')return 3; the     return-1; - } - structtrie{ -     intfail[1005],next[1005][5],end[1005],l,root; +     intNewNode () { -          for(intI=0;i<4; i++) +next[l][i]=-1; Aend[l++]=-1; at         returnL-1;  -     } -     voidClear () { -L=0; -root=NewNode (); -     } in     voidInsertChars[]) { -         intLen=strlen (s), now=Root; to          for(intI=0; i<len;i++){ +             if(Next[now][ch (s[i])]==-1) Next[now][ch (S[i])]=NewNode (); -now=next[now][ch (S[i]); the         } *end[now]=1; $     }Panax Notoginseng     voidbuild () { -std::queue<int>Q; the         intNow ; +          for(intI=0;i<4; i++) A          if(next[root][i]==-1) thenext[root][i]=Root; +          Else{ -fail[next[root][i]]=Root; $ Q.push (Next[root][i]); $          }  -          while(!Q.empty ()) { -now=Q.front (); Q.pop (); the             if(end[fail[now]]==1) end[now]=1; -              for(intI=0;i<4; i++){Wuyi                 if(next[now][i]==-1) next[now][i]=Next[fail[now]][i]; the                 Else{ -fail[next[now][i]]=Next[fail[now]][i]; Wu Q.push (Next[now][i]); -                 } About             } $         }  -     } - }ac; - structmatrix{ A     intn,mx[ the][ the]; + Matrix () {} theMatrix (intx) { -n=x; $          for(intI=0; i<n;i++) the           for(intj=0; j<n;j++) themx[i][j]=0; the     }     the }; -Matrixoperator*(Matrix A,matrix b) { in     intn=A.N; theMatrix c=Matrix (n); the      for(intI=0; i<n;i++) About       for(intj=0; j<n;j++) the        for(intk=0; k<n;k++){ the           intTmp= (Long Long) a.mx[i][k]*b.mx[k][j]%MOD; thec.mx[i][j]= (c.mx[i][j]+tmp)%MOD; +       } -     returnC;  the } BayiMatrix powm (Matrix T,intm) { theMatrix r=Matrix (T.N); the      for(intI=0; i<=t.n;i++) -r.mx[i][i]=1; -      while(m) { the         if(m%2) r=r*T; thet=t*T; theM/=2; the     } -     returnR; the } the intMain () { the     intm;94     intN; the      while(SCANF ("%d%d", &n,&m)! =EOF) { the ac.clear (); the          for(intI=0; i<n;i++){98scanf"%s", s); About Ac.insert (s); -         }101 ac.build ();102Matrix a=Matrix (AC. L);103          for(intI=0; I<ac. l;i++)104            for(intj=0;j<4; j + +) the             if(ac.end[ac.next[i][j]]==-1)106a.mx[i][ac.next[i][j]]++;107A=powm (a,m);108         intans=0;109          for(intI=0; i<a.n;i++) theAns= (ans+a.mx[0][i])%MOD;111printf"%d\n", ans);  the     }113}

POJ 2778 DNA Sequence (ac automata, matrix multiplication)

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.