poj2778DNA Sequence (AC自动机+矩阵快速幂)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Time Limit: 1000MS | Memory Limit: 65536K |
Description
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
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
Sample Input
4 3
AT
AC
AG
AA
Sample Output
36
Source
#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自动机+矩阵快速幂)的更多相关文章
- [poj2778]DNA Sequence(AC自动机+矩阵快速幂)
题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) 解题关键:AC自动机,实际上就是一个状态转移图,注意能少取模就少取模, ...
- poj 2778 DNA Sequence ac自动机+矩阵快速幂
链接:http://poj.org/problem?id=2778 题意:给定不超过10串,每串长度不超过10的灾难基因:问在之后给定的长度不超过2e9的基因长度中不包含灾难基因的基因有多少中? DN ...
- 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 ...
- POJ2778 DNA Sequence(AC自动机+矩阵快速幂)
题目给m个病毒串,问不包含病毒串的长度n的DNA片段有几个. 感觉这题好神,看了好久的题解. 所有病毒串构造一个AC自动机,这个AC自动机可以看作一张有向图,图上的每个顶点就是Trie树上的结点,每个 ...
- 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 ...
- 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 ...
- POJ 2778 DNA Sequence(AC自动机 + 矩阵快速幂)题解
题意:给出m个模式串,要求你构造长度为n(n <= 2000000000)的主串,主串不包含模式串,问这样的主串有几个 思路:因为要不包含模式串,显然又是ac自动机.因为n很大,所以用dp不太好 ...
- POJ2778(SummerTrainingDay10-B AC自动机+矩阵快速幂)
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17160 Accepted: 6616 Des ...
- 考研路茫茫——单词情结 HDU - 2243 AC自动机 && 矩阵快速幂
背单词,始终是复习英语的重要环节.在荒废了3年大学生涯后,Lele也终于要开始背单词了. 一天,Lele在某本单词书上看到了一个根据词根来背单词的方法.比如"ab",放在单词前一般 ...
随机推荐
- QTableWidget简单操作
使用Qt设计师工具,在窗体上添加Table Widget控件,这样就可以使用ui全局变量来调用该控件了. Table Widget控件的应用 (1)设置列数和行数 //设¦¨¨置?列¢D数ºy和¨ª行 ...
- 以字符串形式读取github上.json文件
如下: https://github.com/hpu-spring87/ebooks/blob/master/update.json 如果直接用httpclient读取该URL地址,得到结果是这样的: ...
- CSS3选择器小结
CSS3 选择器小结 一 通用选择器 1 *{}通配选择符(CSS2):适合所有元素对象. 2 E类型(HTML)选择符(CSS1):以文档语言对象类型DOM作为选择符. 3 E#myid是id ...
- 实现Web虚拟现实的最轻松方案—A-Frame框架
问题 随着vr的热度那么web虚拟现实是否可行 1. 实现Web虚拟现实的最轻松方案 A-Frame A-Frame是一款开源的可通过定制HTML元素构建WebVR方案的框架.有了这个框架,Web程序 ...
- 更快的方式实现PHP数组去重(转)
概述 使用PHP的array_unique()函数允许你传递一个数组,然后移除重复的值,返回一个拥有唯一值的数组.这个函数大多数情况下都能工作得很好.但是,如果你尝试在一个大的数组里使用array_u ...
- angular中设置$http的post请求的数据传递格式
ArgularJS的$http方法支持全局设置: $http.defaults.headers.post["Content-Type"] = "application/x ...
- 微信授权登陆接入第三方App(步骤总结)Android
微信授权登陆接入第三方App(步骤总结)Android Android App实现第三方微信登录
- Non-unique Elements
Non-unique Elements You are given a non-empty list of integers (X). For this task, you should return ...
- heritrix 3.2.0 下载
由于archive.org屏蔽,编译完成版本 http://builds.archive.org/maven2/org/archive/heritrix/heritrix/3.2.0/ 无法下载. 现 ...
- c++ 12
一.模板与继承 1.从模板类派生模板子类 2.为模板子类提供基类 二.容器和迭代器 以链表为例. 三.STL概览 1.十大容器 1)向量(vector):连续内存,后端压弹,插删低效 2)列表(lis ...