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. Android开发之UI更新交互机制与实例解析

    android开发过程中,经常需要更新UI的状态和文案等.这是就需要对UI进行 更新.在android中更新UI一般有三种方法,handler机制.RunOnUiThread方法以及AsyncTask ...

  2. Android 自定义View修炼-Android开发之自定义View开发及实例详解

    在开发Android应用的过程中,难免需要自定义View,其实自定义View不难,只要了解原理,实现起来就没有那么难. 其主要原理就是继承View,重写构造方法.onDraw,(onMeasure)等 ...

  3. Chapter 5. The Gradle Wrapper 关于gradle wrapper

    Most tools require installation on your computer before you can use them. If the installation is eas ...

  4. CSS背景颜色、背景图片、平铺、定位、固定

    CSS背景颜色设置 background-color:red;如设置背景颜色为红色: 背景颜色设置支持3种写法: 颜色名 16进制 rgb CSS背景图片颜色设置 background-image:u ...

  5. guid 新建

    var guid = Guid.NewGuid();foreach (var i in new string[] { "P", "N", "B&quo ...

  6. Python开发实战教程(8)-向网页提交获取数据

    来这里找志同道合的小伙伴!↑↑↑ Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知 ...

  7. ubuntu15.10升级时校验和不符的解决方法

    博客: 安卓之家 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 博客园: 追风917 # 错误 # 原因分析 进入/var/lib/apt/lists/partial看下吧: 看到了吧 ...

  8. iOS开发进阶-实现多线程的3种方法

    相关文章链接: 1.多线程简介 2.实现多线程的3种方法 ......待续 前言 在多线程简介中,我已经说明过了,为了提高界面的流畅度以及用户体验.我们务必要把耗时的操作放到别的线程中去执行,千万不要 ...

  9. C#获取数组的行和列数程序代码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  10. 图像处理简单实例[OpenCV 笔记1]

    几个入门的简单程序,和对应的CMakeList, 虽然简单重新测一下写一下也是好的. CMake教程传送门 图像显示 ShowImage.cxx #include <opencv2/opencv ...