POJ-2778
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 12726 | Accepted: 4862 |
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
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
推荐一个网址 :Maxtrix67
/**
题意:给出n个字符串,问长度为m的字符串中有多少没有包含病毒
做法:AC自动机 + 矩阵快速幂
Maxtrix 67 给出的定理8
经典题目8 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值
把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j。 令C=A*A,
那么C(i,j)=ΣA(i,k)*A(k,j),实际上就等于从点i到点j恰好经过2条边的路径数(枚举k为中转点)。
类似地,C*A的第i行第j列就表示从i到j经过3条边的路径数。同理,如果要求经过k步的路径数,
我们只需要二分求出A^k即可。
**/
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <map>
#define MM 10
#define mod 100000
using namespace std;
struct Matrix
{
unsigned long long mat[][];
int n;
Matrix(){}
Matrix(int _n)
{
n = _n;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
mat[i][j] = ;
}
}
}
Matrix operator *(const Matrix &b) const
{
Matrix res = Matrix(n);
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
res.mat[i][j] = ;
for(int k=;k<n;k++)
{
res.mat[i][j] += mat[i][k] * b.mat[k][j];
}
res.mat[i][j] %= mod;
}
}
return res;
}
};
unsigned long long quick_pow(unsigned long long a,int n)
{
unsigned long long res = ;
unsigned long long tmp = a;
while(n)
{
if(n&) res *= tmp;
tmp *= tmp;
n >>= ;
}
return res;
}
Matrix quick_pow(Matrix a,int n)
{
Matrix res = Matrix(a.n);
for(int i=;i<a.n;i++)
{
res.mat[i][i] = ;
}
Matrix tmp = a;
while(n)
{
if(n&) res =res * tmp;
tmp = tmp * tmp;
n >>= ;
}
return res;
}
struct Tire
{
int next[][MM],fail[];
bool end[];
int L,root;
map<char,int>id;
int newnode()
{
for(int i=;i<MM;i++)
{
next[L][i] = -;
}
end[L++] = ;
return L-;
}
void init()
{
L = ;
root = newnode();
id['A'] = ;
id['T'] = ;
id['C'] = ;
id['G'] = ;
}
void insert(char buf[])
{
int now = root; int len = strlen(buf);
for(int i=;i<len;i++)
{
if(next[now][id[buf[i]]] == -)
next[now][id[buf[i]]] = newnode();
now = next[now][id[buf[i]]];
}
end[now] = true;
}
void build()
{
queue<int>que;
int now = root;
fail[root] = root;
for(int i=;i<;i++)
{
if(next[now][i] == -)
next[now][i] = root;
else
{
fail[next[now][i]] = root;
que.push(next[now][i]);
}
}
while(!que.empty())
{
now = que.front();
que.pop();
if(end[fail[now]]) end[now] = true;
for(int i=;i<;i++)
{
if(next[now][i] == -)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
que.push(next[now][i]);
}
}
}
}
Matrix getMatrix()
{
Matrix res = Matrix(L+);
for(int i=;i<L;i++)
{
for(int j=;j<;j++)
{
if(end[next[i][j]] == false && !end[i])
res.mat[i][next[i][j]] ++;
}
}
return res;
}
};
char buf[];
Tire ac;
int main()
{
// freopen("in.txt","r",stdin);
int n,m;
while(~scanf("%d %d",&n,&m))
{
ac.init();
for(int i=;i<n;i++)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
Matrix a = ac.getMatrix();
a = quick_pow(a,m);
unsigned long long res = ;
for(int i=;i<a.n;i++)
{
res += a.mat[][i];
}
cout<<res%mod<<endl;
}
return ;
}
POJ-2778的更多相关文章
- poj 2778 AC自己主动机 + 矩阵高速幂
// poj 2778 AC自己主动机 + 矩阵高速幂 // // 题目链接: // // http://poj.org/problem?id=2778 // // 解题思路: // // 建立AC自 ...
- POJ 2778 (AC自动机+矩阵乘法)
POJ 2778 DNA Sequence Problem : 给m个只含有(A,G,C,T)的模式串(m <= 10, len <=10), 询问所有长度为n的只含有(A,G,C,T)的 ...
- poj 2778 DNA Sequence ac自动机+矩阵快速幂
链接:http://poj.org/problem?id=2778 题意:给定不超过10串,每串长度不超过10的灾难基因:问在之后给定的长度不超过2e9的基因长度中不包含灾难基因的基因有多少中? DN ...
- POJ 2778 DNA Sequence(AC自动机+矩阵快速幂)
题目链接:http://poj.org/problem?id=2778 题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) ...
- POJ 2778 AC自己主动机+矩阵幂 不错的题
http://poj.org/problem?id=2778 有空再又一次做下,对状态图的理解非常重要 题解: http://blog.csdn.net/morgan_xww/article/deta ...
- POJ 2778 DNA Sequence(AC自动机+矩阵)
[题目链接] http://poj.org/problem?id=2778 [题目大意] 给出一些字符串,求不包含这些字符串的长度为n的字符串的数量 [题解] 我们将所有串插入自动机计算match,对 ...
- POJ 2778:DNA Sequence(AC自动机构造矩阵)
http://poj.org/problem?id=2778 题意:有m个病毒DNA,问构造一个长度为n的不带病毒DNA的字符串可以有多少种. 思路:看到这题有点懵,想了挺久题解的思路. 使用AC自动 ...
- POJ 2778 DNA Sequence(AC自动机+矩阵加速)
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9899 Accepted: 3717 Desc ...
- POJ 2778 DNA sequence
QAQ 做完禁忌 又做过文本生成器 这道题就是个水题啦 首先转移方程还是文本生成器的转移方程 但是注意到L很大,但是节点数很小 转移都是固定的,所以我们可以用AC自动机来构造转移矩阵 之后进行矩阵乘法 ...
- POJ 2778 DNA Sequence (AC自己主动机 + dp)
DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...
随机推荐
- BZOJ3669:[NOI2014]魔法森林——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=3669 https://www.luogu.org/problemnew/show/P2387 为了得 ...
- UVA.11384 Help is needed for Dexter (思维题)
UVA.11384 Help is needed for Dexter (思维题) 题意分析 同样水题一道,这回思路对了. 给出数字n,面对一个1,2,3,4--n的数字序列,你可以对他们的部分或者全 ...
- ES6箭头函数总结
1. 箭头函数基本形式 let func = (num) => num; let func = () => num; let sum = (num1,num2) => num1 + ...
- bzoj1656: [Usaco2006 Jan] The Grove 树木 (bfs+新姿势)
题目大意:一个n*m的图中,“.”可走,“X”不可走,“*”为起点,问从起点开始绕所有X一圈回到起点最少需要走多少步. 一开始看到这题,自己脑洞了下怎么写,应该是可过,然后跑去看了题解,又学会了一 ...
- python邮件服务
文件形式的邮件 [python] view plaincopy #!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime ...
- middle中间件
什么事中间件: http://www.crazyant.net/1001.html dd
- 【题解】互不侵犯 SCOI 2005 BZOJ 1087 插头dp
以前没学插头dp的时候觉得这题贼难,根本不会做,学了才发现原来是一裸题. 用二进制表示以前的格子的状态,0表示没放国王,1表示放了国王. 假设当前位置为(x,y),需要记录的是(x-1,y-1)至(x ...
- hive executeTask被interrupt处理
异常信息如下: java.io.IOException: Failed on local exception: java.nio.channels.ClosedByInterruptException ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- 2015/9/10 Python基础(11):错误和异常
程序在执行的过程中会产生异常,出现错误在以前的一个时期是致命的,后来随着程序的发展,使得一些错误的处理方式是柔和的,发生错误会产生一些错误的诊断信息和一些模糊的提示.帮助我们来处理异常.今天将学习Py ...