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. C++数据结构和算法每天一练(线性表)

    #include <iostream> using namespace std;  class  ArrayLinerTable { public:       void InitLine ...

  2. GUI编程笔记(java)06:GUI窗体添加按钮并对按钮添加事件案例

    1.需求:把按钮添加到窗体,并对按钮添加一个点击事件. 步骤: (1)创建窗体对象(2)创建按钮对象(3)把按钮添加到窗体(4)窗体显示 2.编写程序思路: 窗体布局:窗体中组件的排列方式  布局分类 ...

  3. 初建FreeMarker工程

    初建FreeMarker工程 ——@梁WP 背景:听说freemarker可以用来写页面的组件,热衷于编写可重用代码的我,迫不及待地研究了freemarker,不过,在写组件之前,还是先研究一下fre ...

  4. ubuntu自定义服务模板

    根据他人代码修改: #!/bin/sh ### BEGIN INIT INFO # Provides: <pragram name> # Required-Start: $local_fs ...

  5. 核心运营报表无线端数据,pv,uv相关数据,从9月1号开始就没了,为什么?

    问题现象截图 核心运营报表 从获取数据的api的地址可以看出: http://data.51buy.com/json.php?biz=statistic&mod=OrderKeyData&am ...

  6. 在VS中关于MySQL的相关问题

    最近在vs上折腾mysql数据库 遇到了一些小问题,这里记录一下 问题一:数据源选择中没有mysql数据库的选项 解放方法: 1.安装MySql的VS插件(版本请下载最新版)mysql-for-vis ...

  7. c# 获取MAC IP TCP列表

    转载自baidu:http://hi.baidu.com/jackeyrain/item/ff94efcfd5cf3a3099b498e9 namespace Public { public clas ...

  8. python基础知识一

    数 python中有4种类型的数--整数.长整数.浮点数和复数. --2是一个整数 --长整数不过是大一些的整数 --3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里52.3E-4表 ...

  9. C#当中的多线程_线程基础

    前言 最近工作不是很忙,想把买了很久了的<C#多线程编程实战>看完,所以索性把每一章的重点记录一下,方便以后回忆. 第1章 线程基础 1.创建一个线程 using System; usin ...

  10. oracle session 相关优化

    导读: 同学们是不是都用遇到过这种情况,一个业务系统开发期业务并发量只是估算一个值,而系统上线后这个并发量可能会出现溢出或是不够的   情况.在这种情况下我们DBA怎么给出合理的性能优化建议呢?本文就 ...