转载请注明出处: 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. webview 上 postUrl 发送参数过程中数据丢失或错误 的问题

    用到了android 的 webview 来展示页面.webview需要用post来传递参数.于是问题出现了,后台解析中发现参数错误. 之前有因为String 和byte[]转行时,数据丢失的问题,于 ...

  2. python之6-4装饰器.md

    装饰器看的说实话真心郁闷,群里一伙计说了好一会,听得一愣一愣的,查了点资料,又自己试了下,算是明白了一些,记录记录=.=更郁闷的是,博客园的markdown标记支持怎么和为知的不匹配,这转过来的文章很 ...

  3. jdk1.7 JDBC连接SQL Server2008

    路由器网:http://www.ming4.com/news/2355.html Jackie的博客:http://blog.163.com/jackie_howe/blog/static/19949 ...

  4. easyui 添加dialog

    javascript //查看角色所属用户 function roleuser(obj, id) { var C_ID = id; var Url = "/Sys/RoleUserName& ...

  5. (转)C#在父窗口中调用子窗口的过程(无法访问已释放的对象)

    C#在父窗口中调用子窗口的过程: 1. 创建子窗口对象 2. 显示子窗口对象   笔者的程序中,主窗体MainFrm通过菜单调用子窗口ChildFrm.在窗体中定义了子窗口对象,然后在菜单项点击事件中 ...

  6. Using ROWNUM in Oracle

    ROWNUM is an Oracle pseudo column which numbers the rows in a result set. SELECT rownum, table_nameF ...

  7. 自己动手写谷歌API翻译接口

      可以看到,利用GET请求方式,带入某些参数,就会返回一个json数组,QueryString参数如下:     同样的,我们只需要传入这三个参数,就可以获得我们想要的翻译内容,公开方法,代码如下. ...

  8. LeetCode_Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. .NET 使用unity实现依赖注入

    原文地址:http://www.cnblogs.com/wujy/p/3317795.html 一:理论部分 依赖注入:这是 Ioc 模式的一种特殊情况,是一种基于改变对象的行为而不改变类的内部的接口 ...

  10. 转:PHP的(Thread Safe与Non Thread Safe)

    在安装xdebug到时候你会有有TS和NTS版本的选择,在以前还有VC6和VC9的版本.如果你没有根据你目前的服务器的状况选择对应的版本的话,那么xdebug是安装不成功的. 一.如何选择 php5. ...