# [题解]POJ2778 DNA Sequence

3/8/2017来源：ASP.NET技巧人气：8892

## Description

It’s well known that DNA Sequence is 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 may mean that the animal may have a genetic disease. Until now scientists have 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 is a sequence that consist of A, C, T and G，and the length of sequences is a given integer n.

## Input

First line contains two integer m (0 <= m <= 10), n (1 <= n <=2000000000). Here, m is 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 is not larger than 10.

## Output

An integer, the number of DNA sequences, mod 100000.

4 3 AT AC AG AA

36

## Solution

f[0][1]=1; //初始化 for(int i=1;i<=n;i++){ //n为输入的字符串长度 for(int j=1;j<=num;j++){ //num为AC自动机中节点总数，1节点是根 if(!T[j].flag&&f[i-1][j]){ //如果该节点合法并且当前点的答案不为0（如果当前节点答案为0就没必要更新了，更新等于没更新） for(int k=1;k<=4;k++){ //枚举4种字母 int p=j; while(!T[p].next[k])p=T[p].fail; //如果不匹配就沿着失配边走（为了防止死循环，已经提早把T[0].next全部赋为1） (f[i][T[p].next[k]]+=f[i-1][j])%=mod; //更新其他点的答案 } } } }

#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const int maxn=110,mod=100000; struct node{ int next[5],fail,flag; }T[maxn]; struct matrix{ LL a[maxn][maxn]; int l,r; matrix(){memset(a,0,sizeof a);} }temp,c,I; int n,m,num=1,que[maxn],ans,head,tail,Hash[256]; char ch[12]; void Insert(char *s){ int p=1,len=strlen(s); for(int i=0;i<len;i++){ if(!T[p].next[Hash[s[i]]])T[p].next[Hash[s[i]]]=++num; p=T[p].next[Hash[s[i]]]; } T[p].flag=true; } void Bfs(){ //构造AC自动机的fail边，顺便标记不合法节点 memset(que,head=tail=0,sizeof que); que[++tail]=1; while(head<tail){ int x=que[++head]; for(int i=1;i<=4;i++){ if(T[x].next[i]){ int p=T[x].fail; while(!T[p].next[i])p=T[p].fail; T[T[x].next[i]].fail=T[p].next[i]; if(T[T[p].next[i]].flag)T[T[x].next[i]].flag=true; que[++tail]=T[x].next[i]; } } } } matrix mul(matrix x,matrix y){ matrix re; re.l=x.l;re.r=y.r; for(int i=1;i<=re.l;i++){ for(int j=1;j<=re.r;j++){ for(int k=1;k<=x.r;k++){ (re.a[i][j]+=x.a[i][k]*y.a[k][j])%=mod; } } } return re; } matrix pow(matrix x,int y){ matrix re=I; while(y){ if(y&1)re=mul(re,x); x=mul(x,x);y>>=1; } return re; } int main(){ Hash['A']=1;Hash['C']=2;Hash['T']=3;Hash['G']=4; for(int i=1;i<=4;i++)T[0].next[i]=1; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ scanf("\n%s",ch); Insert(ch); } Bfs();temp.l=1;c.l=c.r=I.l=I.r=temp.r=num; for(int i=1;i<=num;i++)I.a[i][i]=1; for(int i=1;i<=num;i++){ //转移矩阵 if(T[i].flag)continue; for(int j=1;j<=4;j++){ int p=i; while(!T[p].next[j])p=T[p].fail; c.a[i][T[p].next[j]]++; } } temp.a[1][1]=1; temp=mul(temp,pow(c,m)); for(int i=1;i<=num;i++){ //统计答案 if(!T[i].flag){ (ans+=temp.a[1][i])%=mod; } } printf("%d\n",ans); return 0; }

• 电脑主机模板下载

• 欧美打猎网站模板

• 棒球网站模板

• 医院公司网站模板

• 欧美企业汽车模板

• 娱乐休闲音乐模板

• 欧美音乐网站模板

• 欧美音乐网站模板

• 旅游网站模板

• 鱼模板下载

• 模特时尚网站模板

• 音乐网站模板

• 1
• 2
• 3