地址: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. Kickoff - 创造可扩展的,响应式的网站

    Kickoff 是一个轻量级的前端框架,用于创建可扩展的,响应式的网站.作为前端开发人员,我们工作的类型越来越多样化.Kickoff 旨在帮助您在所有项目保持一致的结构和风格,无需添加其他框架. 在线 ...

  2. PHP插入header('content-type:text/html;charset="utf-8')和error_reporting()

    1.header PHP文件插入header("Content-type: text/html; charset=utf-8");相当于页面里面的<meta http-equ ...

  3. js一些代码方法

    概要 1.替换json对象中属性值(包括子对象) 2.兼容多个$库写法(zepto与jquery) 3.闭包保持变量的做法 详情 1.替换json对象中属性值(包括子对象) //替换json对象属性值 ...

  4. C#开源项目汇总

    Discuz nt: 一个开源的论坛项目.估计你现在逛过大大小小的论坛没有成百上千,也有几十个吧,其中是个论坛6个以上都是Discuz(以前大部分都是php版的),现 在官方也早就放出了DotNet( ...

  5. 使用js制作一般网站首页图片轮播效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. CSS3动画(个人理解)

    随着学习的深入,越来越觉得Css3动画的重要,虽然JQ自定义动画和动画回调函数必须掌握,但是css3动画做起来更加绚丽,更加方便!1.常规使用1.1 使用transition属性,一般我们是配合hov ...

  7. Python将Excel生成SHP

    #!/usr/bin/env python # -*- coding: utf-8 -*- import gdal import xlrd import shapefile # open the ex ...

  8. MSCRM 2013/2015 Ribbon Editor

    由于新版本2015的解决方案与之前有变化,因此许多老的Tools已经不能使用,推荐给大家新的Ribbon Editor Tool. 下载地址: http://www.develop1.net/publ ...

  9. SharePoint 2010 匿名访问开启后不能访问Allitems.aspx或DisplayForm.aspx

    Body: Full Credit goes to Pet Stilgoe: http://www.petestilgoe.com/2010/02/allowed-anonymous-access-o ...

  10. 【Leafletjs】6.Control.Loading推展-在地图上边框添加加载动态条

    在已有的Control.Loading控件基础上结合CSS3 animation属性实现 .nz-loading .nz-loader { display: block; -webkit-animat ...