地址:http://poj.org/problem?id=2778

题目:

DNA Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15453   Accepted: 5964

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自动机,然后所有节点可看做状态转移图。呃,懒得画图了,你们可以百度下。然后根据转移图建立矩阵,之后快速幂即可。
  之前一直忘了把end【x】为1的节点的所有子节点的end标记为1,wa的不省人事。
 #include <queue>
#include <cstring>
#include <cstdio>
using namespace std; const long long mod=1e5;
struct MM
{
int r,c;
long long mx[][];
MM(int rr=,int cc=){r=rr,c=cc;}
friend MM operator *(MM ta,MM tb)
{
MM tc(ta.r,tb.c);
for(int i=;i<tc.r;i++)
for(int j=;j<tc.c;j++)
{
tc.mx[i][j]=;
for(int k=;k<tb.r;k++)
tc.mx[i][j]=(tc.mx[i][j]+ta.mx[i][k]*tb.mx[k][j])%mod;
}
return tc;
}
friend MM operator ^(MM ta,int num)
{
MM ret(ta.r,ta.c); //r==c
memset(ret.mx,,sizeof ret.mx);
for(int i=;i<ta.r;i++) ret.mx[i][i]=; //µ¥Î»¾ØÕó
while(num)
{
if(num&)
ret=ta*ret;
num>>=;
ta=ta*ta;
}
return ret;
}
}mm;
struct AC_auto
{
const static int LetterSize = ;
const static int TrieSize = *; int tot,root,fail[TrieSize],end[TrieSize],next[TrieSize][LetterSize]; int newnode(void)
{
memset(next[tot],-,sizeof(next[tot]));
end[tot] = ;
return tot++;
} void init(void)
{
tot = ;
root = newnode();
} int getidx(char x)
{
if(x=='A')return ;
else if(x=='C')return ;
else if(x=='G')return ;
else return ;
} void insert(char *ss)
{
int len = strlen(ss);
int now = root;
for(int i = ; i < len; i++)
{
int idx = getidx(ss[i]);
if(next[now][idx] == -)
next[now][idx] = newnode();
now = next[now][idx];
}
end[now]=;
} void build(void)
{
queue<int>Q;
fail[root] = root;
for(int i = ; i < LetterSize; i++)
if(next[root][i] == -)
next[root][i] = root;
else
fail[next[root][i]] = root,Q.push(next[root][i]);
while(Q.size())
{
int now = Q.front();Q.pop();
if(end[fail[now]])end[now]=;
for(int i = ; i < LetterSize; i++)
if(next[now][i] == -) next[now][i] = next[fail[now]][i];
else
fail[next[now][i]] = next[fail[now]][i],Q.push(next[now][i]);
}
} int buildmm(int n)
{
int cnt = ;
mm.c=mm.r=tot;
for(int i = ; i < tot; i++)
for(int j = ; j < ; j++)
if(!end[next[i][j]])
mm.mx[i][next[i][j]]++;
mm=mm ^ n;
for(int i = ; i < tot; i++)
cnt = (cnt + mm.mx[][i]) % mod;
return cnt;
}
}; AC_auto ac;
char sa[];
int main(void)
{
int n,m;
scanf("%d%d",&n,&m);
ac.init();
for(int i=;i<=n;i++)
scanf("%s",sa),ac.insert(sa);
ac.build();
printf("%d\n",ac.buildmm(m));
return ;
}

poj 2278 DNASequnce AC自动机的更多相关文章

  1. POJ 1625 Censored!(AC自动机+DP+高精度)

    Censored! Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6956   Accepted: 1887 Descrip ...

  2. poj 1625 (AC自动机好模版,大数好模版)

    题目 给n个字母,构成长度为m的串,总共有n^m种.给p个字符串,问n^m种字符串中不包含(不是子串)这p个字符串的个数. 将p个不能包含的字符串建立AC自动机,每个结点用val值来标记以当前节点为后 ...

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

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

  4. DNA Sequence POJ - 2778 (ac自动机 + 快速幂)

    题意: 给出患病的DNA序列,问序列长度为n的,且不包含患病的DNA序列有多少种 解析: 以给出的患病DNA序列建trie树  患病结点要用flag标记 对于长度为n的序列 位置i有四种 情况A  C ...

  5. POJ 1625 Censored! [AC自动机 高精度]

    Censored! Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 9793   Accepted: 2686 Descrip ...

  6. 【原创】AC自动机小结

    有了KMP和Trie的基础,就可以学习神奇的AC自动机了.AC自动机其实就是在Trie树上实现KMP,可以完成多模式串的匹配.           AC自动机 其实 就是创建了一个状态的转移图,思想很 ...

  7. 转自kuangbin的AC自动机(赛前最后一博)

    有了KMP和Trie的基础,就可以学习神奇的AC自动机了.AC自动机其实就是在Trie树上实现KMP,可以完成多模式串的匹配.           AC自动机 其实 就是创建了一个状态的转移图,思想很 ...

  8. HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)

    Description Aliens on planet Pandora also write computer programs like us. Their programs only consi ...

  9. POJ 2778 DNA Sequence(AC自动机+矩阵加速)

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9899   Accepted: 3717 Desc ...

随机推荐

  1. HTML5基础之textarea 和lable

    HTML基础:表单<form/>中文本域textarea 和lable <textarea rows="行数" cols="列数">文本 ...

  2. 也来谈谈wap端瀑布流布局

    Definition 瀑布流布局,在视觉上表现为参差不齐的多栏布局,随着页面滚动条向下滚动,新数据不断被加载进来. 瀑布流对于图片的展现,是高效而具有吸引力的,用户一眼扫过的快速阅读模式可以在短时间内 ...

  3. 利用Canvas实现360度浏览

    前言:最近几个月来到新公司,主要从事移动端方面的开发,有时候也挺忙挺累的,于是就好一段时间没写博客了.其实自己在这几个月里,自己对canvas以及createjs和egret都有了一定程度上的认识与掌 ...

  4. 部署基于国际版Azure的SharePoint三层架构服务器场

    前言 微软Azure国际版已经很普及了,这里没有用国内版(世纪互联),用的是国际版,当然是由于公司性质的缘故.这里一步步图文的方式,分享给大家创建Azure国际版的SharePoint三层架构的过程, ...

  5. Android Sqlite基本命令

    要查看数据库,首先必须要找到db文件,如果拷贝到电脑上,查看的方法比较多,在手机上,用命令查看比较直接和方便. 首先要找到数据库的位置,一般数据库时存放在程序的私有目录,所以要获取root权限. 确保 ...

  6. 如何正确使用Cocoapods

    ➠更多技术干货请戳:听云博客 一.介绍Cocoapods Cocoapods是引入为项目引入新血液的接口,只有引入了新血液,功能才可以多样化,进而满足不同的消费群体.使用Cocoapods可以方便日后 ...

  7. 认识Activity,创建第一个android应用-Hello Word

    2016-04-05 配置好Java.eclipse和Android环境就花费了一天时间.下载SDK真是费了不少时间.现在终于找到解决SDK更新的好方法了(更新自己电脑上的hosts文件,就可以使用G ...

  8. Getting Started with ASP.NET Web API 2 (C#)

    By Mike Wasson|last updated May 28, 2015 7556 of 8454 people found this helpful Print   Download Com ...

  9. php示例代码之类似于C#中的String.Format方法

    php示例代码之类似于C#中的String.Format方法 原文来自于  http://stackoverflow.com/questions/1241177/c-string-format-equ ...

  10. 安装SqlServer的时候性能计数器注册表配置单元一致性失败的解决办法

    http://www.2cto.com/database/201204/126772.html