DNA Sequence
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14758 Accepted: 5716

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自动机,然后把每个字符串的最后一位所在的节点标黑。(就是把mark变为1) 然后用AC自动机建立矩阵array[i][j]表示从i走到j的方案数,然后利用矩阵乘法的性质,用快速幂求出矩阵的M次幂即可。 代码依然丑:
 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 1010
#define LL long long
#define Mod 100000 struct node
{
int x,fail;
int son[];
bool mark;
}t[Maxn];int tot=; struct Matrix
{
LL a[][];
}ay,ut; void upd(int x)
{
t[x].mark=;
memset(t[x].son,,sizeof(t[x].son));
} Matrix h;
void clear(int id)
{
for(int i=;i<=tot;i++)
for(int j=;j<=tot;j++)
if(id==) ay.a[i][j]=;
else h.a[i][j]=;
} int m,n;
char s[Maxn]; void read_trie()
{
scanf("%s",s+);
int len=strlen(s+);
int now=;
for(int i=;i<=len;i++)
{
int ind;
if(s[i]=='A') ind=;
else if(s[i]=='T') ind=;
else if(s[i]=='C') ind=;
else ind=;
if(!t[now].son[ind])
{
t[now].son[ind]=++tot,upd(tot);
}
now=t[now].son[ind];
if(i==len) t[now].mark=;
}
} queue<int > q;
void build_AC()
{
while(!q.empty()) q.pop();
q.push();
while(!q.empty())
{
int now=q.front();q.pop();
int f=t[now].fail;
for(int i=;i<=;i++)
{
if(t[now].son[i])
{
q.push(t[now].son[i]);
t[t[now].son[i]].fail=now?t[f].son[i]:;
if(t[t[t[now].son[i]].fail].mark) t[t[now].son[i]].mark=;
}
else t[now].son[i]=t[f].son[i];
if(t[t[now].son[i]].mark!=) ay.a[now][t[now].son[i]]++;
}
}
} Matrix mul(Matrix a,Matrix b)
{
clear();
for(int i=;i<=tot;i++)
for(int j=;j<=tot;j++)
for(int k=;k<=tot;k++)
{
h.a[i][j]=(h.a[i][j]+a.a[i][k]*b.a[k][j])%Mod; }
return h;
} void qpow(int k)
{
while(k)
{
if(k&) ut=mul(ut,ay);
ay=mul(ay,ay);
k>>=;
}
} void init()
{
scanf("%d%d",&m,&n);
tot=;upd();
for(int i=;i<=m;i++)
{
read_trie();
}
clear();
build_AC();
for(int i=;i<=tot;i++)
for(int j=;j<=tot;j++)
ut.a[i][j]=i==j?:; qpow(n);
LL ans=;
for(int i=;i<=tot;i++) ans=(ans+ut.a[][i])%Mod;
printf("%I64d\n",ans);
} int main()
{
init();
return ;
}

[POJ2778]

以前的code:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std; const int Maxn=;
const int Mod=(int)1e5;
char s[Maxn];
int q[Maxn];
long long ay[Maxn][Maxn],sum[Maxn][Maxn];
int n,m,tot,len; struct node
{
int son[],cnt,fail,mark;
}t[Maxn]; void floy()
{
tot=;
for(int i=;i<=Maxn;i++)
{
t[i].cnt=;t[i].mark=;
for(int j=;j<=;j++) t[i].son[j]=;
}
memset(q,,sizeof(q));
} void read_trie()
{
tot=;
int i,j;
scanf("%d%d",&m,&n);
for(i=;i<=m;i++)
{
int x,ind;
scanf("%s",s+);
len=strlen(s+);
x=;
for(j=;j<=len;j++)
{
if(s[j]=='A') ind=;
else if(s[j]=='C') ind=;
else if(s[j]=='T') ind=;
else if(s[j]=='G') ind=;
if(!t[x].son[ind]) t[x].son[ind]=++tot;
x=t[x].son[ind];
t[x].cnt++;
if(j==len) t[x].mark=;
}
}
} void build_AC()
{
int i,j,x,y;
q[++q[]]=;
//for(i=1;i<=4;i++)
//if(t[0].son[i]) q[++q[0]]=t[0].son[i];
for(i=;i<=q[];i++)
{
x=q[i];
y=t[x].fail;
for(j=;j<=;j++)
{
if(t[x].son[j])
{
//t[t[x].son[j]].fail=t[y].son[j];
q[++q[]]=t[x].son[j];
t[t[x].son[j]].fail=x?t[y].son[j]:x;
if(t[t[t[x].son[j]].fail].mark) t[t[x].son[j]].mark=;
}
else t[x].son[j]=t[y].son[j];
if(!t[t[x].son[j]].mark) ay[x][t[x].son[j]]++;
}
}
} void MatrixMult(long long a[Maxn][Maxn],long long b[Maxn][Maxn])
{
int i,j,k;
long long c[Maxn][Maxn];
memset(c,,sizeof(c));
for(i=;i<=tot;i++)
for(j=;j<=tot;j++)
for(k=;k<=tot;k++)
c[i][j]+=a[i][k]*b[k][j];
for(i=;i<=tot;i++)
for(j=;j<=tot;j++)
a[i][j]=c[i][j]%Mod;
} long long MatrixPow(int k)
{
int i,j;
for(i=;i<=tot;i++)
for(j=;j<=tot;j++)
sum[i][j]=(i==j);
while(k)
{
if(k&) MatrixMult(sum,ay);
MatrixMult(ay,ay);
k>>=;
}
long long ans=;
for(i=;i<=tot;i++) ans+=sum[][i];
return ans%Mod;
} int main()
{
floy();
read_trie();
build_AC();
printf("%I64d\n",MatrixPow(n));
return ;
}

[POJ2778_2]

2016-06-23 16:22:32

【POJ2778】AC自动机+矩阵乘法的更多相关文章

  1. 【bzoj1444】[Jsoi2009]有趣的游戏 AC自动机+矩阵乘法

    题目描述 输入 注意 是0<=P 输出 样例输入 样例输出 题解 AC自动机+矩阵乘法 先将所有字符串放到AC自动机中,求出Trie图. 然后构建邻接矩阵:如果x不是某个字符串的末位置,则x连向 ...

  2. 【poj2778-DNA Sequence】AC自动机+矩阵乘法

    题意: (只含AGCT)给定m个病毒串,让你构造一个长度为n的字符串(也只含有AGCT),问有多少种方案.n很大:1<=n<=2000000000 题解: 用病毒串建立AC自动机(num个 ...

  3. [BZOJ 1009] [HNOI2008] GT考试 【AC自动机 + 矩阵乘法优化DP】

    题目链接:BZOJ - 1009 题目分析 题目要求求出不包含给定字符串的长度为 n 的字符串的数量. 既然这样,应该就是 KMP + DP ,用 f[i][j] 表示长度为 i ,匹配到模式串第 j ...

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

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

  5. hdu2243之AC自动机+矩阵乘法

    考研路茫茫——单词情结 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. bzoj 2553: [BeiJing2011]禁忌 AC自动机+矩阵乘法

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=2553 题解: 利用AC自动机的dp求出所有的转移 然后将所有的转移储存到矩阵中,进行矩阵 ...

  7. BZOJ 1009 GT考试 (AC自动机 + 矩阵乘法加速dp)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1009 题意: 准考证号为\(n\)位数\(X_1X_2....X_n(0<=X_ ...

  8. 洛谷 P4569 - [BJWC2011]禁忌(AC 自动机+矩阵乘法)

    题面传送门 又好久没做过 AC 自动机的题了,做道练练手罢( 首先考虑对于某个固定的字符串怎样求出它的伤害,我们考虑贪心,每碰到出现一个模式串就将其划分为一段,最终该字符串的代价就是划分的次数.具体来 ...

  9. DNA Sequence - POJ 2778(AC自动机+矩阵乘法)

    题目大意:DNA序列是有 ATGC 组成的,现在知道一些动物的遗传片段有害的,那么如果给出这些有害的片段,能否求出来所有长度为 N 的基因中有多少是不包含这些有害片段的.   分析:也是断断续续做了一 ...

随机推荐

  1. GDB的non-stop模式

    线程调试必杀技 - GDB的non-stop模式   作者:破砂锅 开源的GDB被广泛使用在Linux.OSX.Unix和各种嵌入式系统(例如手机),这次它又带给我们一个惊喜. 多线程调试之痛 调试器 ...

  2. The Class Loader Hierarchy--转载

    Class loaders in the Application Server runtime follow a delegation hierarchy that is illustrated in ...

  3. iOS组件化思路-大神博客研读和思考

    一.大神博客研读 随着应用需求逐步迭代,应用的代码体积将会越来越大,为了更好的管理应用工程,我们开始借助CocoaPods版本管理工具对原有应用工程进行拆分.但是仅仅完成代码拆分还不足以解决业务之间的 ...

  4. django开发框架之jumpserver

    发现一个不错的开源堡垒机 jumpserver: https://github.com/ibuler/jumpserver 最开始看的是jumpserver2.0.0 版本,具体的实现方式是: 1. ...

  5. Bash中的数组

    变量:$VAR或者${VAR} 数组:${VAR[$i]} 打印整个数组:echo ${VAR[@]} 统计数组元素个数:echo ${#VAR[@]} 从文件读入数组(按行读入):VAR=(`cat ...

  6. index页面数据展示为设定的命名

    数据库表里面字段的值想用另一种命名形式展示,如1是 是,2是 否  解决方法: 用到formatter ,{field: 'params', title: '参数', width: 100, sort ...

  7. 使用jquery.validate.js实现boostrap3的校验和验证

    使用jquery.validate.js实现boostrap3的校验和验证 boostrap3验证框架 jquery.validate.js校验表单 >>>>>>& ...

  8. Liferay环境搭建实录

    1. 安装Liferay IDE:打开Eclipse的Help-->Eclipse Marketplace,在下图所示搜索框中输入liferay,回车搜索: 搜索结果如下图所示: 点击insta ...

  9. Bootstrap 开关(switch)控件需要注意的问题

    远程文档地址:http://www.bootcss.com/p/bootstrap-switch/ 先上lz遇到的小坑:自古无图无真相的原则 上面代码注释掉后 就是下面这个图片效果!然后加载顺序也要注 ...

  10. 详解android:scaleType属性

    详解android:scaleType属性 转自:http://blog.csdn.net/encienqi/article/details/7913262    http://juliaailse. ...