POJ2778(SummerTrainingDay10-B AC自动机+矩阵快速幂)
DNA Sequence
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17160 | Accepted: 6616 |
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
//2017-08-10
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define ll long long using namespace std; const int K = ;
const int N = ;
const int M = ;
const int MOD = ; struct Matrix{
ll a[M*M][M*M];
int r, c;
}mat, tmp; Matrix multi(Matrix x, Matrix y)//矩阵乘法
{
Matrix z;
memset(z.a, , sizeof(z.a));
z.r = x.r, z.c = y.c;
for(int i = ; i < x.r; i++){
for(int k = ; k < x.c; k++)//加速优化
{
if(x.a[i][k] == ) continue;
for(int j = ; j< y.c; j++)
z.a[i][j] = (z.a[i][j] + (x.a[i][k] * y.a[k][j]) % MOD) % MOD;
}
}
return z;
} void Matrix_pow(int n)//矩阵快速幂
{
Matrix tmp;
tmp.c = mat.c;
tmp.r = mat.r;
memset(tmp.a, , sizeof(tmp.a));
for(int i = ; i < tmp.c; i++)
tmp.a[i][i] = ;
while(n){
if(n & )
tmp = multi(tmp, mat);
mat = multi(mat, mat);
n >>= ;
}
int ans = ;
for(int i = ; i < tmp.c; i++)
ans = (ans + tmp.a[][i]) % MOD;
printf("%d\n", ans);
} struct AC_automation
{
//node nodes[N], *root, *superRoot, *cur;
int nex[M*M][], fail[M*M], match[M*M];
int root, CNT;
int newNode(){
for(int i = ; i < K; i++)
nex[CNT][i] = -;
match[CNT++] = ;
return CNT-;
}
int Hash(char ch)
{
if(ch == 'A')return ;
else if(ch == 'C')return ;
else if(ch == 'T')return ;
else if(ch == 'G')return ;
}
void init(){
CNT = ;
root = newNode();
}
void Insert(char s[]){//向字典树中插入一个字符串
int n = strlen(s);
int cur = root;
for(int i = ; i < n; i++){
int p = Hash(s[i]);
if(nex[cur][p] == -)
nex[cur][p] = newNode();
cur = nex[cur][p];
}
match[cur]++;
}
void build(){//构建自动机
queue<int> que;
fail[root] = root;
for(int i = ; i < K; i++){
if(nex[root][i] == -)
nex[root][i] = root;
else{
fail[nex[root][i]] = root;
que.push(nex[root][i]);
}
}
while(!que.empty()){
int cur = que.front();
if(match[fail[cur]])match[cur] = ;
que.pop();
for(int i = ; i < K; i++){
if(nex[cur][i] == -){
nex[cur][i] = nex[fail[cur]][i];
}else{
fail[nex[cur][i]] = nex[fail[cur]][i];
que.push(nex[cur][i]);
}
}
}
}
void to_marix(){
memset(mat.a, , sizeof(mat.a));
mat.r = mat.c = CNT;
for(int i = ; i < CNT; i++){
for(int j = ; j < ; j++)
if(!match[nex[i][j]])
mat.a[i][nex[i][j]]++;
}
// for(int i = 0; i < CNT; i++){
// for(int j = 0; j < CNT; j++)
// cout<<mat.a[i][j]<<" ";
// cout<<endl;
// }
}
}; char str[M];
AC_automation ac; int main()
{
int n, m;
while(scanf("%d%d", &m, &n)!=EOF)
{
ac.init();
for(int i = ; i < m; i++){
scanf("%s", str);
ac.Insert(str);
}
ac.build();
ac.to_marix();
Matrix_pow(n);
} return ;
}
POJ2778(SummerTrainingDay10-B AC自动机+矩阵快速幂)的更多相关文章
- [poj2778]DNA Sequence(AC自动机+矩阵快速幂)
题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) 解题关键:AC自动机,实际上就是一个状态转移图,注意能少取模就少取模, ...
- 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树上的结点,每个 ...
- poj2778 ac自动机+矩阵快速幂
给m个子串,求长度为n的不包含子串的母串数,最直接的应该是暴搜,肯定tle,考虑用ac自动机 将子串建成字典树,通过next表来构造矩阵,然后用矩阵快速幂求长度为n的数量 邻接矩阵https://we ...
- poj2778DNA Sequence (AC自动机+矩阵快速幂)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud DNA Sequence Time Limit: 1000MS Memory ...
- HDU 2243考研路茫茫——单词情结 (AC自动机+矩阵快速幂)
背单词,始终是复习英语的重要环节.在荒废了3年大学生涯后,Lele也终于要开始背单词了. 一天,Lele在某本单词书上看到了一个根据词根来背单词的方法.比如"ab",放在单词前一般 ...
- HDU 2243 考研路茫茫――单词情结 ——(AC自动机+矩阵快速幂)
和前几天做的AC自动机类似. 思路简单但是代码200余行.. 假设solve_sub(i)表示长度为i的不含危险单词的总数. 最终答案为用总数(26^1+26^2+...+26^n)减去(solve_ ...
- POJ - 2778 ~ HDU - 2243 AC自动机+矩阵快速幂
这两题属于AC自动机的第二种套路通过矩阵快速幂求方案数. 题意:给m个病毒字符串,问长度为n的DNA片段有多少种没有包含病毒串的. 根据AC自动机的tire图,我们可以获得一个可达矩阵. 关于这题的t ...
- 考研路茫茫——单词情结 HDU - 2243 AC自动机 && 矩阵快速幂
背单词,始终是复习英语的重要环节.在荒废了3年大学生涯后,Lele也终于要开始背单词了. 一天,Lele在某本单词书上看到了一个根据词根来背单词的方法.比如"ab",放在单词前一般 ...
随机推荐
- 10_python_函数进阶
一.函数参数-动态参数 形参:位置参数.默认值参数.动态参数 动态参数分为两种:动态接收位置参数 *args .动态接收关键字参数 *kwargs 1. *args def chi(*foo ...
- spring-boot集成thymeleaf。
thymeleaf是前台页面展示,原来一直是jsp,jsp中包含很多服务器端的逻辑,逐渐淘汰.同样功能的还有freemarker.孰好孰坏不予评价,只做简单实现. 1.基本思路 (1)pom.xml中 ...
- centos 部署.netcore 开发环境
.netcore 2.0的安装,安装前,先参考官方文档 https://www.microsoft.com/net/core#linuxcentos 先做微软的签名校验工作 # sudo rpm -- ...
- 本地主机不安装oracle客户端--访问远程oracle数据库
在不安装oracle客户端情况下用sqlplus连接数据库: 1.去官网下载 http://www.oracle.com/technetwork/topics/winx64soft-089540.ht ...
- 通过XMLHttpRequest和jQuery两种方式实现ajax
一.XMLHttpRequest实现获取数据 不使用jQuery实现页面不刷新获取内容的方式,我们这里采用XMLHttpRequest原生代码实现:js代码如下: //1.获取a节点,并为其添加Onc ...
- Git使用(1)
安装git完成后 1.首先配置你的用户信息,用于体现在你的提交记录中包含your name and your email git config --global user.name "you ...
- JAVA框架之Hibernate【Hibernate缓存详解】
1.缓存介绍 Hibernate中提供了两级Cache,第一级别的缓存是Session级别的缓存,它是属于事务范围的缓存.这一级别的缓存由hibernate管理的,一般情况下无需进行干预:第二级别的缓 ...
- Configuration problem: Failed to import bean definitions from relative location
问题现象: 最近开始做新需求,然后在Tomcat上部署项目时,出现了如下报错: [12-05 09:54:27,161 ERROR] ContextLoader.java:351 - Context ...
- 高可用的MongoDB集群-实战篇
1.概述 最近有同学和网友私信我,问我MongoDB方面的问题:这里我整理一篇博客来赘述下MongoDB供大家学习参考,博客的目录内容如下: 基本操作 CRUD MapReduce 本篇文章是基于Mo ...
- 记一次使用mybatis进行like 模糊查询遇到的问题
"bdate like '#{date}%' and ..." 最开始这样写的· 将传入的参数和%用单引号包起来,但是这会报错 java.sql.SQLException: Par ...