线性代数(矩阵乘法):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四个字符) ...
随机推荐
- iOS原生CIFilter创建二维码
iOS原生CIFilter创建二维码 2016-05-31 未来C iOS原生CIFilter创建二维码 关于二维码生成,网上也是有很多,很早以前的第三方库大多数都是通过C++写,也是有的如zxing ...
- [Redis] C#中使用redis
C#中使用redis 首先打开Visual Studio建立一个简单的控制台应用程序,我这里暂时使用的VS2013的版本. 然后通过Nuget进行安装Redis常用组件ServiceStack.Red ...
- 绝对炫的幻灯片插件-SKITTER
绝对炫的幻灯片插件-SKITTER 所属分类:媒体-幻灯片和轮播图,图片展示,滑块和旋转 Includes code source // Styles <link href="css/ ...
- struts_ognl详解
- Android-自定义多TAB悬浮控件实现蘑菇街首页效果
因为项目的一些需求需要用到此种展现方式. 找了市面上大部分有类似功能的应用. 基本思路嵌套ScrollView 转换事件分发给listview 实现. 但是此种方案有个缺点. 在ScrollVie ...
- TOM大师脚本01-查找未建索引的外键
[oracle@Oracle11g 2016]$ cat 022201.sql column columns format a30 word_wrappedcolumn tablename forma ...
- iOS app闪退的一般原因
1.函数无限递归爆栈(表视图返回Cell和返回行高的方法互相调用)2.某对象无法解析某个方法(没做类型转换.或者代理没实现某个方法)3.访问了某个已经被释放的对象(ARC之后不太有)4.从Bundle ...
- [转]一个备份MySQL数据库的简单Shell脚本
本文翻译自 iSystemAdmin 的 <A Simple Shell Script to Backup MySQL Database> Shell脚本是我们写不同类型命令的一种脚本,这 ...
- WHU 1572 Cyy and Fzz (AC自动机 dp )
题意: 给出n个串,求任意长度为m的字符串包含串的个数的期望.(n<=8,m<=14,给定串的长度不超过12). Solution: 首先可以想到应该用概率DP,我们需要至少3维,dp[i ...
- TextView控件
1.手动创建(不建议): TextView tv = new TextView(this); tv.setContent("你好"); setContentView(tv); 2. ...