首页 > 学院 > 开发设计 > 正文

[题解]POJ2778 DNA Sequence

2019-11-06 05:26:40
字体:
来源:转载
供稿:网友

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.

Sample Input

4 3 AT AC AG AA

Sample Output

36

Solution

题目大意:给定m个字符串和一个长度n,求不包含这m个字符串的长度为n的字符串个数mod 100000的值,字符串中只含’A’、’C’、’T’、’G’。 做法: 首先构造一下AC自动机我们就会发现,题目要求的就是在AC自动机上跑n次同时不经过任何一个表示字符串的点的方案总数。仔细想一想会发现,有两种点不合法:一是直接表示字符串的点,二是fail边连向表示字符串的点的点。所以我们就可以DP,在构造AC自动机的时候给不合法的点打上标记。用fi,j表示跑了i次到达j节点的合法路径总数,dp如下:

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; //更新其他点的答案 } } }}

但是这样肯定不行,n有2∗109,所以我们可以用矩阵乘法优化当前DP。矩阵还是容易推的,实在不懂看看代码就行了。 代码:

#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;}
上一篇:Scala求和例子

下一篇:leetcode-Happy Number

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表