地址: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. sizzle源码分析 (2)ID 类 tag querySelectorAll 快速匹配

    不是所有的选择器都需要去分词,生成相应的匹配函数,这样流程比较复杂,当浏览器具备原生的方法去匹配元素是,没有理由不优先匹配,下面看看进入Sizzle后,它是怎么优先匹配这些元素的: function ...

  2. Angularjs 的 ngInfiniteScroll 的使用方法

    Angularjs 的 ngInfiniteScroll 的使用方法 一.介绍 ngInfiniteScroll 是一个 AngularJS 的扩展指令,实现了网页的无限滚动的功能,也就是相当于页面滚 ...

  3. 最近喜欢听的英文歌——Because Of You - Kelly Clarkson

    没了解过歌曲背景,总觉得像是一首女儿唱给母亲的歌= =也许是我的错觉 I will not make the same mistakes that you did 我不会重复你犯过的错误 I will ...

  4. 不直接登录SharePoint服务器,通过远程直接部署WSP解决方案包

    在这之前不得不说一下Sysinternals,他最初是一个网站,网站上会经常发布一些有用的系统扩展工具,之后运营这个网站的公司被微软收购.像非常著名的Procmon,AutoRuns.Process ...

  5. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q69-Q71)

    Question 69 You are designing an extranet site using SharePoint 2010. This site must allow employees ...

  6. oracle应该安装在什么版本的linux下

    今天想在我的ubuntu上安装oracle 11g r2,在网上所有了一些教程,然后找到了oracle官网网站的Operating System Requirements,内容如下: Operatin ...

  7. Git 分支管理策略

    分支管理策略 下面我们来说一下一般企业中开发一个项目的分支策略: 主分支 master 开发分支 develop 功能分支 feature 预发布分支  release bug 分支 fixbug 其 ...

  8. zookeeper入门讲解事例

    zookeeper使用和原理探究(一) zookeeper介绍zookeeper是一个为分布式应用提供一致性服务的软件,它是开源的Hadoop项目中的一个子项目,并且根据google发表的<Th ...

  9. equals()方法

    equals()方法是根类Object中的一个方法,子类可以根据需要重写该方法(比如:String类). 一.Object类中的equals()方法实现如下: public boolean equal ...

  10. 【代码笔记】iOS-UIScrollerView里有两个tableView

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...