DNA Sequence
 

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
  
思路是这样的:把所有病毒片段放入AC自动机中,建立fail数组。如果一个状态的fail为病毒节点,则他自己也为病毒节点。最后按边建矩阵,快速幂。
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn=;
const int mod=;
typedef unsigned long long ull;
struct Matrix{
int n;
ull mat[maxn][maxn];
Matrix(int n_,int on=){
n=n_;memset(mat,,sizeof(mat));
if(on)for(int i=;i<=n;i++)mat[i][i]=;
}
Matrix operator *(Matrix a){
Matrix ret(n);
unsigned long long l;
for(int i=;i<=n;i++)
for(int k=;k<=n;k++){
l=mat[i][k];
for(int j=;j<=n;j++)
(ret.mat[i][j]+=l*a.mat[k][j]%mod)%=mod;
}
return ret;
}
Matrix operator ^(long long k){
Matrix ret(n,);
while(k){
if(k&)
ret=ret**this;
k>>=;
*this=*this**this;
}
return ret;
}
}; struct AC_automation{
bool tag[maxn];
int cnt,rt,ch[maxn][],fail[maxn];
AC_automation(){
memset(tag,,sizeof(tag));
memset(fail,,sizeof(fail));
memset(ch,,sizeof(ch));cnt=rt=;
} int ID(char c){
if(c=='A')return ;
else if(c=='C')return ;
else if(c=='G')return ;
else return ;
} void Insert(char *s){
int len=strlen(s),p=rt;
for(int i=;i<len;i++)
if(ch[p][ID(s[i])])
p=ch[p][ID(s[i])];
else
p=ch[p][ID(s[i])]=++cnt;
tag[p]=true;
} void Build(){
queue<int>q;
for(int i=;i<;i++)
if(ch[rt][i])
fail[ch[rt][i]]=rt,q.push(ch[rt][i]);
else
ch[rt][i]=rt; while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<;i++)
if(ch[x][i]){
fail[ch[x][i]]=ch[fail[x]][i];
tag[ch[x][i]]|=tag[fail[ch[x][i]]];
q.push(ch[x][i]);
}
else
ch[x][i]=ch[fail[x]][i];
}
} void Solve(int k){
Matrix A(cnt);
for(int i=;i<=cnt;i++)
for(int j=;j<;j++)
if(!tag[i]&&!tag[ch[i][j]])
A.mat[ch[i][j]][i]+=;
A=A^k;
long long ans=;
for(int i=;i<=cnt;i++)
ans+=A.mat[i][];
printf("%lld\n",ans%mod);
}
}ac;
char s[maxn]; int main(){
#ifndef ONLINE_JUDGE
//freopen("","r",stdin);
//freopen("","w",stdout);
#endif
int tot,n;
scanf("%d%d",&tot,&n);
while(tot--){
scanf("%s",s);
ac.Insert(s);
}
ac.Build();
ac.Solve(n);
return ;
}

线性代数(矩阵乘法):POJ 2778 DNA Sequence的更多相关文章

  1. POJ 2778 DNA Sequence (AC自动机,矩阵乘法)

    题意:给定n个不能出现的模式串,给定一个长度m,要求长度为m的合法串有多少种. 思路:用AC自动机,利用AC自动机上的节点做矩阵乘法. #include<iostream> #includ ...

  2. POJ 2778 DNA Sequence(AC自动机+矩阵加速)

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9899   Accepted: 3717 Desc ...

  3. POJ 2778 DNA Sequence (ac自动机+矩阵快速幂)

    DNA Sequence Description It's well known that DNA Sequence is a sequence only contains A, C, T and G ...

  4. poj 2778 DNA Sequence AC自动机DP 矩阵优化

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11860   Accepted: 4527 Des ...

  5. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

  6. POJ 2778 DNA sequence

    QAQ 做完禁忌 又做过文本生成器 这道题就是个水题啦 首先转移方程还是文本生成器的转移方程 但是注意到L很大,但是节点数很小 转移都是固定的,所以我们可以用AC自动机来构造转移矩阵 之后进行矩阵乘法 ...

  7. poj 2778 DNA Sequence AC自动机

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11860   Accepted: 4527 Des ...

  8. poj 2778 DNA Sequence ac自动机+矩阵快速幂

    链接:http://poj.org/problem?id=2778 题意:给定不超过10串,每串长度不超过10的灾难基因:问在之后给定的长度不超过2e9的基因长度中不包含灾难基因的基因有多少中? DN ...

  9. POJ 2778 DNA Sequence(AC自动机+矩阵快速幂)

    题目链接:http://poj.org/problem?id=2778 题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) ...

随机推荐

  1. RSA 加解密 签名 示例

    import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStrea ...

  2. Store update, insert, or delete statement affected an unexpected number of rows ({0}).

    问题描述 Store update, insert, or delete statement affected an unexpected number of rows ({0}). Entities ...

  3. HTML5 WebAudioAPI(四)--绘制频谱图2

    绘制分析器数组所有数据.本文内容,承接上文 1.800宽度绘制 var url='../content/audio/海阔天空.mp3'; if (!window.AudioContext) { ale ...

  4. Android中全局搜索(QuickSearchBox)详解

    http://blog.csdn.net/mayingcai1987/article/details/6268732 1. 标题: 应用程序如何全面支持搜索 2. 引言: 如果想让某个应用程序支持全局 ...

  5. 使用 logback + slf4j 进行日志记录

    此处主要介绍maven web工程下如何使用 logback + slf4j  进行日志记录. logback主要包含三个组成部分:Loggers(日志记录器).Appenders(输出目的在).La ...

  6. java 进制.

    /* 整数的'3'种表现形式: 1,十进制. 2,八进制. 3,十六进制. */ public class IntegerDemo { public static void main(String[] ...

  7. PHP 开发API接口 注册,登录,查询用户资料

    服务端 <?php require 'conn.php'; header('Content-Type:text/html;charset=utf-8'); $action = $_GET['ac ...

  8. Oracle 左连接、右连接、全外连接、(+)号作用、inner join(等值连接) (转载)

    Oracle  外连接 (1)左外连接 (左边的表不加限制)       (2)右外连接(右边的表不加限制)       (3)全外连接(左右两表都不加限制) 外连接(Outer Join) oute ...

  9. mysql 数据sqoop到hive 步骤

    1.hive建表 hive是支持分区的,但是这次建表没有写分区. CREATE TABLE `cuoti_rpt` ( `COURSE_ID` string, `NAME` string, `PERI ...

  10. Python下调用json.dumps中文显示问题解决办法

    json.dumps在默认情况下,对于非ascii字符生成的是相对应的字符编码,而非原始字符,例如: import json js = json.loads('{"haha": & ...