转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

DNA Sequence
Time Limit: 1000MS   Memory Limit: 65536K

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

 
题意
构造一个长度为n的DNA序列,要求其中不得出现m个禁止的字符串中的任意一个
一道很明显的矩阵快速幂的题。先通过AC自动机得出一个邻接矩阵,然后快速幂。
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
#define REP(A,X) for(int A=0;A<X;A++)
#define MAXN 100010 int p[MAXN][];
int tail[MAXN];
int fail[MAXN];
int root,tot;
const long long MOD =;
struct Matrix{
int n;
int mat[][];
Matrix(){}
Matrix(int _n){
n=_n;
REP(i,n)
REP(j,n)mat[i][j]=;
}
void init()
{
REP(i,tot)
REP(j,tot)mat[i][j]=;
}
void unit()
{
REP(i,tot)
REP(j,tot)mat[i][j]=i==j?:;
}
Matrix operator *(const Matrix &a)const {
Matrix ret(n);
REP(i,n)
REP(j,n)
REP(k,n)
{
int tmp=(long long)mat[i][k]*a.mat[k][j]%MOD;
ret.mat[i][j]=(ret.mat[i][j]+tmp)%MOD;
}
return ret;
}
};
int newnode()
{
REP(i,)p[tot][i]=-;
tail[tot++]=;
return tot-;
}
void init()
{
tot=;
root=newnode();
}
int a[MAXN];
void insert(char *s){
int len=strlen(s);
REP(i,len)
{
if(s[i]=='A')a[i]=;
else if(s[i]=='C')a[i]=;
else if(s[i]=='G')a[i]=;
else if(s[i]=='T')a[i]=;
}
int now= root ;
REP(i,len)
{
if(p[now][a[i]]==-)p[now][a[i]]=newnode();
now=p[now][a[i]];
}
tail[now]++;
}
void build()
{
int now=root;
queue<int >q;
fail[root]=root;
REP(i,){
if(p[root][i]==-){
p[root][i]=root;
}
else {
fail[p[root][i]]=root;
q.push(p[root][i]);
}
}
while(!q.empty())
{
now =q.front();
q.pop();
if(tail[fail[now]])tail[now]=;
REP(i,){
if(p[now][i]==-){
p[now][i]=p[fail[now]][i];
}else{
fail[p[now][i]]=p[fail[now]][i];
q.push(p[now][i]);
}
}
}
}
char s[MAXN];
Matrix Mat;
int main()
{
ios::sync_with_stdio(false);
int n,m;
while(cin>>m>>n){
init();
REP(i,m){
cin>>s;
insert(s);
}
build();
Mat.n=tot;
Mat.init();
REP(i,tot){
REP(j,){
if(!tail[p[i][j]])Mat.mat[i][p[i][j]]++;
}
}
Matrix tmp(tot);
tmp.unit();
while(n){
if(n&)tmp=tmp*Mat;
Mat=Mat*Mat;
n>>=;
}
int ans=;
REP(i,tot)ans+=tmp.mat[][i];
ans%=MOD;
cout<<ans<<endl; }
return ;
}

代码君

 

poj2778DNA Sequence (AC自动机+矩阵快速幂)的更多相关文章

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

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

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

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

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

    Description It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's ve ...

  4. POJ2778 DNA Sequence(AC自动机+矩阵快速幂)

    题目给m个病毒串,问不包含病毒串的长度n的DNA片段有几个. 感觉这题好神,看了好久的题解. 所有病毒串构造一个AC自动机,这个AC自动机可以看作一张有向图,图上的每个顶点就是Trie树上的结点,每个 ...

  5. POJ 2778 DNA Sequence (ac自动机+矩阵快速幂)

    DNA Sequence Description It's well known that DNA Sequence is a sequence only contains A, C, T and G ...

  6. DNA Sequence POJ - 2778 AC自动机 && 矩阵快速幂

    It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's very useful to ...

  7. POJ 2778 DNA Sequence(AC自动机 + 矩阵快速幂)题解

    题意:给出m个模式串,要求你构造长度为n(n <= 2000000000)的主串,主串不包含模式串,问这样的主串有几个 思路:因为要不包含模式串,显然又是ac自动机.因为n很大,所以用dp不太好 ...

  8. POJ2778(SummerTrainingDay10-B AC自动机+矩阵快速幂)

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17160   Accepted: 6616 Des ...

  9. 考研路茫茫——单词情结 HDU - 2243 AC自动机 && 矩阵快速幂

    背单词,始终是复习英语的重要环节.在荒废了3年大学生涯后,Lele也终于要开始背单词了. 一天,Lele在某本单词书上看到了一个根据词根来背单词的方法.比如"ab",放在单词前一般 ...

随机推荐

  1. BZOJ 3926 && ZJOI 2015 诸神眷顾的幻想乡 (广义后缀自动机)

    3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec Memory Limit: 512 MB Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽 ...

  2. poj1083 贪心

    K - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB     64bit ...

  3. hdu 1019 n个数的最小公倍数

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  4. JAVA GUI 工具

    Java GUI图形界面开发工具   上大学那会儿比较主流的Java图形开发插件是:Visual Editor 和 SWT Designer, 不久又出了个Jigloo, 但去官网看了下发现这个东西也 ...

  5. iscroll-lite.js源码注释

    /*! iScroll v5.1.2 ~ (c) 2008-2014 Matteo Spinelli ~ http://cubiq.org/license */ (function (window, ...

  6. 【转】简析SynchronousQueue,LinkedBlockingQueue,ArrayBlockingQueue

    转载地址:http://blog.csdn.net/mn11201117/article/details/8671497 SynchronousQueue SynchronousQueue是无界的,是 ...

  7. 163k地方门户网站系统团购定时结束限量控制

    #coding=utf8 #!/usr/bin/env python # 网站自动审核系统 import pymssql import re import sys import datetime im ...

  8. Netbeans使用Xdebug调试的配置

    在phpinfo()信息里找到php.ini的位置并打开php.ini在文档最后添加如下代码: 注释原来xdebug配置 xdebug.remote_enable=onxdebug.remote_ha ...

  9. Android-PullToRefresh 使用心得

    目前下拉刷新已经满大街都是,在自己的应用如果不使用这个模式的话,出门都不好意思和人家打招呼,该文章就是简单探讨下针对于 github 上的这个开源项目的使用心得. 为什么是它?因为在 stackove ...

  10. 使用WCF实现SOA面向服务编程—— 架构设计

    原文地址:http://www.cnblogs.com/leslies2/archive/2011/03/29/1997889.html SOA本身就是一种面向企业级服务的系统架构,简单来说,SOA就 ...