地址: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. Electron - 创建跨平台的桌面客户的应用程序

    Electron 框架的前身是 Atom Shell,可以让你写使用 JavaScript,HTML 和 CSS 构建跨平台的桌面应用程序.它是基于io.js 和 Chromium 开源项目,并用于在 ...

  2. HoverTree菜单0.1.3新增效果

    HoverTree菜单0.1.3增加弹出菜单的动态效果,可以是动态下拉,也可以是动态淡入. 效果请看:http://keleyi.com/jq/hovertree/demo/demo.0.1.3.ht ...

  3. LigerUI一个前台框架增、删、改asp.net代码的实现

    先上代码:前台代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  4. 关于html页面head标签顺序

    基本上head就这几个标签么: <meta>.<link>.<title>.<script>.<style>.<base>. 它 ...

  5. jQuery 重要的知识点归纳

    jQuery 对象 jQuery 对象就是通过 jQuery 包装 DOM 对象后产生的对象. jQuery 对象是 jQuery 独有的. 只有 jQuery 对象才能使用 jQuery 的方法,在 ...

  6. sharepoint2010问卷调查(1)-实现问卷的图片调查(采用JS实现)

      1. 首先建立个图片库上传图片 2. 采用:评估范围(一系列选项或 Likert 范围)制作,如下图: http://win-i07fillcfom:8003/DocLib2/1.jpg http ...

  7. 根据werservice代码用CXF生成WSDL

    原文:http://hongyegu.iteye.com/blog/619147,谢谢! import org.apache.cxf.tools.java2ws.JavaToWS; import ne ...

  8. Android环境搭建和编写helloworld

    一.配置jdk环境(学过java的请无视) 1.安装jdk jdk下载地址:http://www.oracle.com/technetwork/cn/java/javase/downloads/jdk ...

  9. CGAffineTransform方法汇总

    CGAffineTransform是二维的仿射变换,可以进行位移,旋转,缩放,CGAffineTransform实际上是一个矩阵. CGAffineTransform { CGFloat a, b, ...

  10. C#复习⑤

    C#复习⑤ 2016年6月19日 22:39 Main Inheritance 继承 1.继承的语法结构 class A { // base class int a; public A() {...} ...