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 ...
随机推荐
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- HDOJ(HDU).1015 Safecracker (DFS)
HDOJ(HDU).1015 Safecracker [从零开始DFS(2)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1 ...
- SRM16 B-2(DP)
老鼠和洞按坐标排序 f[i][j]表示前i个洞进j只老鼠的最短距离 比赛的时候强行分三类去推式子,推是推出来了,也看出来是可以用三个单调队列去优化的,但是太繁琐了,要我敲我真没办法T^T 赛后经 on ...
- caffe中使用crop_size剪裁训练图片
layer { name: "data" type: "Data" top: "data" top: "label" i ...
- 【java】AES加密解密|及Base64的使用
转载自:http://www.cnblogs.com/arix04/archive/2009/10/15/1511839.html AES加解密算法,使用Base64做转码以及辅助加密: packag ...
- linux查看文件相关指令
以下内容整理自以下两篇文章: http://www.cnblogs.com/xilifeng/archive/2012/10/13/2722596.html Linux 查看文件内容的命令 http: ...
- [LeetCode] 12. Integer to Roman ☆☆
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- IntelliJ IDEA使用技巧 (类比pycharm)
第1章 课程介绍 1)下载及安装2)界面介绍 ... ①界面中左右最边上的小窗口(旋转了90度的字体)都自带了下标,win下快捷键alt+index切换窗口 ②演示效果:模拟时钟,快捷键如,0到10 ...
- 省队集训 Day3 吴清华
[题目大意] 给网格图,共有$n * n$个关键节点,横向.纵向距离均为$d$,那么网格总长度和宽度均为$(n+1) * d + 1$,最外围一圈除了四角是终止节点.要求每个关键节点都要通过线连向终止 ...
- 透彻理解Spring事务设计思想之手写实现(山东数漫江湖)
前言 事务,是描述一组操作的抽象,比如对数据库的一组操作,要么全部成功,要么全部失败.事务具有4个特性:Atomicity(原子性),Consistency(一致性),Isolation(隔离性),D ...