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",放在单词前一般 ...
随机推荐
- php 简单分页
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- LoadRunner利用ODBC编写MySql脚本
最近做了几周的LoadRunner测试,有一些心得,记录下来,以便以后查找. LoadRunner测试数据库是模拟客户端去连接数据库服务器,因此,需要协议(或者说驱动的支持).LoadRunner本身 ...
- EF有外键的查询
modelBuilder.Entity<ActionMenu>().ToTable("ActionMenu"); modelBuilder.Entity<Acti ...
- Auto Install Workflow Manager 1.0
Write-Host "- Begining Download Service Bus..." Start /W "c:\Program Files\Microsoft\ ...
- linux下ifconfig, DNS以及route配置
转载:http://blog.csdn.net/wangjingfei/article/details/5283632/ 熟悉使用ifconfig 会非常方便. ifconfig eth0 新ip 然 ...
- Intel 凌动 D525 产品参数Intel 凌动 Z3735F 产品参数
https://item.taobao.com/item.htm?spm=a230r.1.14.8.kauehT&id=40450541158&ns=1&abbucket=19 ...
- vijos1777 引水入城
描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N行M列的矩形,其中每个格子都代表一座城市,每座城市都有一个海拔高度. 为了使居民们都尽可能 ...
- jquery datepicker日期控件用法
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.c ...
- Reverse Nodes in k-Group 解答
Question Given a linked list, reverse the nodes of a linked list k at a time and return its modified ...
- safari浏览器cookie问题
这个题目可能有点大了,这里主要讨论一种解决safari浏览器阻止第三方cookie问题. 场景 公司存在多个域名(a.com,b.com,co.com)这些域名应该统一 ...