DNA Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9889   Accepted: 3712

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

Source

 
 
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; const int N=;
const int mod=; struct Trie{
int ok;
int fail;
int next[];
void Init(){
ok=;
fail=-;
memset(next,-,sizeof(next));
}
}a[N]; char wrd[];
char str[N];
int n,m,cnt,q[N]; int find(char ch){
switch(ch){
case 'A':return ;
case 'C':return ;
case 'T':return ;
case 'G':return ;
}
return ;
} void InsertTrie(char *str){
int p=;
for(int i=;str[i]!='\0';i++){
int id=find(str[i]);
if(a[p].ok)
break;
if(a[p].next[id]==-){
a[p].next[id]=cnt++;
a[cnt-].Init();
}
p=a[p].next[id];
}
a[p].ok++;
} void AC_automation(){
int head=,tail=;
q[tail++]=;
int cur=,tmp;
while(head<tail){
cur=q[head++];
for(int i=;i<;i++){
tmp=a[cur].next[i];
if(tmp!=-){
if(cur==)
a[tmp].fail=;
else{
a[tmp].fail=a[a[cur].fail].next[i];
if(a[a[tmp].fail].ok)
a[tmp].ok++;
}
q[tail++]=a[cur].next[i];
}else{
if(cur==)
a[cur].next[i]=;
else
a[cur].next[i]=a[a[cur].fail].next[i];
}
}
}
} struct Matrix{
long long m[][];
}; Matrix init,unit; void Init(){
memset(init.m,,sizeof(init.m));
for(int i=;i<cnt;i++)
if(!a[i].ok)
for(int j=;j<;j++){
if(a[a[i].next[j]].ok==)
init.m[i][a[i].next[j]]++;
}
//if(a[*a[i].next[j]].ok==0)
//init.m[i][*a[i].next[j]]++;
for(int i=;i<cnt;i++)
for(int j=;j<cnt;j++)
unit.m[i][j]=(i==j)?:;
} Matrix Mul(Matrix a,Matrix b){
Matrix c;
for(int i=;i<cnt;i++)
for(int j=;j<cnt;j++){
c.m[i][j]=;
for(int k=;k<cnt;k++)
c.m[i][j]=(a.m[i][k]*b.m[k][j])%mod;
c.m[i][j]%=mod;
}
return c;
} Matrix Pow(Matrix a,Matrix b,int k){
while(k){
if(k&){
b=Mul(a,b);
}
a=Mul(a,a);
k>>=;
}
return b;
} int main(){ freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){
cnt=;
a[].Init();
for(int i=;i<n;i++){
scanf("%s",wrd);
InsertTrie(wrd);
}
AC_automation();
Init();
Matrix res=Pow(init,unit,m);
long long ans=;
for(int i=;i<cnt;i++)
if(a[i].ok==)
ans=(ans+res.m[][i])%mod;
cout<<ans<<endl;
}
return ;
}

POJ 3691 DNA Sequence (AC自动机 + 矩阵 有bug,待修改)的更多相关文章

  1. poj 2778 DNA Sequence ac自动机+矩阵快速幂

    链接:http://poj.org/problem?id=2778 题意:给定不超过10串,每串长度不超过10的灾难基因:问在之后给定的长度不超过2e9的基因长度中不包含灾难基因的基因有多少中? DN ...

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

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

  3. poj 2778 DNA Sequence AC自动机DP 矩阵优化

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11860   Accepted: 4527 Des ...

  4. POJ 2778 DNA Sequence ( AC自动机、Trie图、矩阵快速幂、DP )

    题意 : 给出一些病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 分析 : 这题搞了我真特么久啊,首先你需要知道的前置技能包括 AC自动机.构建Trie图.矩阵快速幂,其中矩 ...

  5. POJ2278 DNA Sequence —— AC自动机 + 矩阵优化

    题目链接:https://vjudge.net/problem/POJ-2778 DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Tota ...

  6. [poj2778]DNA Sequence(AC自动机+矩阵快速幂)

    题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) 解题关键:AC自动机,实际上就是一个状态转移图,注意能少取模就少取模, ...

  7. POJ2778 DNA Sequence(AC自动机 矩阵)

    先使用AC自动机求得状态转移关系,再建立矩阵,mat[i][j]表示一步可从i到j且i,j节点均非终止字符的方案数,则此矩阵的n次方表示n步从i,到j的方法数. #include<cstdio& ...

  8. POJ 3691 DNA repair(AC自动机+DP)

    题目链接 能AC还是很开心的...此题没有POJ2778那么难,那个题还需要矩阵乘法,两个题有点相似的. 做题之前,把2778代码重新看了一下,回忆一下当时做题的思路,回忆AC自动机是干嘛的... 状 ...

  9. poj 2778 DNA Sequence AC自动机

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11860   Accepted: 4527 Des ...

  10. 【距离GDOI:128天】【POJ2778】DNA Sequence(AC自动机+矩阵加速)

    已经128天了?怎么觉得上次倒计时150天的日子还很近啊 ....好吧为了把AC自动机搞透我也是蛮拼的..把1030和这道题对比了无数遍...最终结论是...无视时间复杂度,1030可以用这种写法解. ...

随机推荐

  1. C++运算符重载(成员函数方式)

    一.运算符重载 C++中预定义的运算符的操作对象只能是基本数据类型,实际上,对于很多用户自定义类型,也需要有类似的运算操作.如果将C++中这些现存的运算符直接作用于用户自定义的类型数据上,会得到什么样 ...

  2. CVPR14 图像检索papers

    CVPR14年关于图像检索方面的papers,汇总成一个list,方便阅读. 图像检索 Triangulation embedding and democratic aggregation for i ...

  3. centos6.8安装具有ngx_cache_purge模块的nginx1.10.3

    CentOS-6.8 安装 Nginx1.10.3Nginx 环境准备:安装Nginx需要完成以下依赖的安装 1.gcc 安装:yum install gcc-c++ 2.PCRE pcre-deve ...

  4. 解决Ubuntu下的Eclipse打开Windows编写的java代码的中文乱码

    其实所有的中文乱码 问题都是编码不同所导致的.这里要想让eclipse能正常显示出汉字,就要修改它的字符编码 步骤如下: 1 ,点击菜单栏中的Window(窗口),选择Preferences(首选项) ...

  5. PHP语言基础之MySql 05 By ACReaper

    PHP的基本语法学完后,我们马上学下PHP如何和MySql进行交互.PHP和MySql进行交互的API可以分为两类,一类是面向过程的,一类是面向对象的,面向对象的我们等复习完面向对象再介绍,现在先介绍 ...

  6. ajax与java前后台传值及数据表查询解决一个bug的问题

    前台选中某些表,确定提交到后台,偶尔会报500错误,通过排查发现:由于后台代码写的不严谨,导致前台选中的表名如果全不存在的话就会导致后台走异常报500错误,所以决定在前台先对数据进行一次过滤,使至少有 ...

  7. 《iOS Human Interface Guidelines》——Search Bar

    搜索栏 搜索栏接收用户输入用于搜索的文本(例如以下,带有占位文本). API NOTE 查看UISearchBar学习怎样在你的代码中定义搜索栏.查看UISearchDisplayController ...

  8. Android的硬件抽象层模块编写规范

    硬件抽象层模块编写规范 ​ Android系统的硬件抽象层以模块的形式来管理各个硬件訪问接口.每个硬件模块都相应有一个动态链接库文件.这些动态链接库文件的命令须要符合一定的规范.同一时候,在系统内部. ...

  9. SSIS包部署错误之运行环境设置

    在SQLServer代理处新建了一个job,步骤里面的操作是指向一个SSIS模型包 执行,报错如下 根据报错信息是64bit,于是做出修改如下图 再次执行 OK

  10. Node.js程序在node-windows中不能运行

      Node.js程序部分功能在命令行中运行良好,但在node-windows中不能运行,怎么回事? 答:路径问题. 请看如下的描述: My script runs fine on it's own, ...