线性代数(矩阵乘法):POJ 2778 DNA Sequence
Description
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
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
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的更多相关文章
- POJ 2778 DNA Sequence (AC自动机,矩阵乘法)
题意:给定n个不能出现的模式串,给定一个长度m,要求长度为m的合法串有多少种. 思路:用AC自动机,利用AC自动机上的节点做矩阵乘法. #include<iostream> #includ ...
- POJ 2778 DNA Sequence(AC自动机+矩阵加速)
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9899 Accepted: 3717 Desc ...
- 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 ...
- poj 2778 DNA Sequence AC自动机DP 矩阵优化
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11860 Accepted: 4527 Des ...
- POJ 2778 DNA Sequence (AC自己主动机 + dp)
DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...
- POJ 2778 DNA sequence
QAQ 做完禁忌 又做过文本生成器 这道题就是个水题啦 首先转移方程还是文本生成器的转移方程 但是注意到L很大,但是节点数很小 转移都是固定的,所以我们可以用AC自动机来构造转移矩阵 之后进行矩阵乘法 ...
- poj 2778 DNA Sequence AC自动机
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11860 Accepted: 4527 Des ...
- poj 2778 DNA Sequence ac自动机+矩阵快速幂
链接:http://poj.org/problem?id=2778 题意:给定不超过10串,每串长度不超过10的灾难基因:问在之后给定的长度不超过2e9的基因长度中不包含灾难基因的基因有多少中? DN ...
- POJ 2778 DNA Sequence(AC自动机+矩阵快速幂)
题目链接:http://poj.org/problem?id=2778 题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) ...
随机推荐
- Red Hat Enterprise Linux 6安装步骤
首先,准备安装环境,此次实验是在VMware Workstation虚拟机环境下来实现的,下面就开始安装: 点击Create a New Vitrual Machine来新建一个虚拟机,选择自定义安装 ...
- Java 原始数据类型转换
在开发中经常遇到数据类型转换的问题,大多数都是拿来强制转换,强制转换可能会出现你意想不到的问题: int a = -1; 我们经过多重转换之后:int b = (int)(char)(byte) a ...
- CakePHP之请求与响应对象
请求与响应对象 请求与响应对象在 CakePHP 2.0 是新增加的.在之前的版本中,这两个对象是由数组表示的,而相关的方法是分散在RequestHandlerComponent,Router,Dis ...
- (转)function($){}(window.jQuery) 是什么意思?
function(){}(); (function(){})(); 这两个是self-invoking anonymous 自调匿名函数,用这类的方法,能强制使匿名函数成为表达式,把不合法变成合法. ...
- git语言
安装完成后,需要进一步设置用户名和email.因为git是分布式版本控制工具,因此需要每台开发机自报家门. $ git config --global user.name "Your Nam ...
- oracle 10g 恢复dmp文件。
1. 在winxp下,安装10g,默认选择,一路ok.(安装前自检出现dhcp警告,可直接忽略) 2.命令行,在xp下,输入sqlplus,即可启动,登陆用 sqlplus / as sysdba 用 ...
- 【转】iOS-Core-Animation-Advanced-Techniques(五)
原文:http://www.cocoachina.com/ios/20150105/10829.html 图层时间和缓冲 图层时间 时间和空间最大的区别在于,时间不能被复用 -- 弗斯特梅里克 在上面 ...
- 342. Power of Four
题目: Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example ...
- c++primerplus(第六版)编程题——第3章(数据类型)
声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. 工程命名和文件命名可以命名成易识 ...
- asp.net中后台javaScrip的使用
ClientScriptManager csm = Page.ClientScript; //Script标记靠近<form>标签 //csm.Register ...