POJ2278 DNA Sequence —— AC自动机 + 矩阵优化
题目链接:https://vjudge.net/problem/POJ-2778
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 18479 | Accepted: 7112 |
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
题意:
给出m个DNA序列,问长度为n且不含这m个序列的DNA有多少个?
题解:
1.把这m个序列插入到AC自动机中。
2.根据自动机中各个状态之间的关系,构成一张邻接矩阵A,但需要去除与“结束点”有关的边,这样就能保证不含有给出的序列。
3.长度为n,那么答案就是 A^n 中,初始状态那一行之和。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e5;
const int MAXN = +; int Size;
int Map[];
struct MA
{
int mat[][];
void init()
{
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
mat[i][j] = (i==j);
}
}; MA operator*(const MA &x, const MA &y)
{
MA ret;
memset(ret.mat, , sizeof(ret.mat));
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
for(int k = ; k<Size; k++)
ret.mat[i][j] += (1LL*x.mat[i][k]*y.mat[k][j])%MOD, ret.mat[i][j] %= MOD;
return ret;
} MA qpow(MA x, int y)
{
MA s;
s.init();
while(y)
{
if(y&) s = s*x;
x = x*x;
y >>= ;
}
return s;
} struct Trie
{
const static int sz = , base = 'A';
int next[MAXN][sz], fail[MAXN], end[MAXN];
int root, L;
int newnode()
{
for(int i = ; i<sz; i++)
next[L][i] = -;
end[L++] = false;
return L-;
}
void init()
{
L = ;
root = newnode();
}
void insert(char buf[])
{
int len = strlen(buf);
int now = root;
for(int i = ; i<len; i++)
{
if(next[now][Map[buf[i]]] == -) next[now][Map[buf[i]]] = newnode();
now = next[now][Map[buf[i]]];
}
end[now] = true;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ; i<sz; i++)
{
if(next[root][i] == -) next[root][i] = root;
else fail[next[root][i]] = root, Q.push(next[root][i]);
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
end[now] |= end[fail[now]]; //当前串的后缀是否也包含单词
for(int i = ; i<sz; i++)
{
if(next[now][i] == -) next[now][i] = next[fail[now]][i];
else fail[next[now][i]] = next[fail[now]][i], Q.push(next[now][i]);
}
}
} int query(int n)
{
MA s;
memset(s.mat, , sizeof(s.mat));
for(int i = ; i<L; i++)
{
if(end[i]) continue; //存在单词的状态没有后继
for(int j = ; j<sz; j++)
{
if(end[next[i][j]]) continue; //存在单词的状态没有前驱
s.mat[i][next[i][j]]++; // i到next[i][j]的路径数+1。注意,当next[i][j]==root时,路径数很可能大于1。
}
} int ret = ;
Size = L;
s = qpow(s, n);
for(int i = ; i<L; i++) //答案为:初始状态到各个状态(包括初始状态)的路径数之和。
ret = (ret+s.mat[][i])%MOD;
return ret;
}
}; Trie ac;
char buf[];
int main()
{
Map['A'] = ; Map['C'] = ; Map['G'] = ; Map['T'] = ; //离散化
int n, m;
while(scanf("%d%d", &m,&n)!=EOF)
{
ac.init();
for(int i = ; i<=m; i++)
{
scanf("%s", buf);
ac.insert(buf);
}
ac.build();
int ans = ac.query(n);
printf("%d\n", ans);
}
return ;
}
POJ2278 DNA Sequence —— AC自动机 + 矩阵优化的更多相关文章
- poj 2778 DNA Sequence ac自动机+矩阵快速幂
链接:http://poj.org/problem?id=2778 题意:给定不超过10串,每串长度不超过10的灾难基因:问在之后给定的长度不超过2e9的基因长度中不包含灾难基因的基因有多少中? DN ...
- [poj2778]DNA Sequence(AC自动机+矩阵快速幂)
题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) 解题关键:AC自动机,实际上就是一个状态转移图,注意能少取模就少取模, ...
- POJ2778 DNA Sequence(AC自动机 矩阵)
先使用AC自动机求得状态转移关系,再建立矩阵,mat[i][j]表示一步可从i到j且i,j节点均非终止字符的方案数,则此矩阵的n次方表示n步从i,到j的方法数. #include<cstdio& ...
- POJ 2778 DNA Sequence (AC自动机,矩阵乘法)
题意:给定n个不能出现的模式串,给定一个长度m,要求长度为m的合法串有多少种. 思路:用AC自动机,利用AC自动机上的节点做矩阵乘法. #include<iostream> #includ ...
- 【距离GDOI:128天】【POJ2778】DNA Sequence(AC自动机+矩阵加速)
已经128天了?怎么觉得上次倒计时150天的日子还很近啊 ....好吧为了把AC自动机搞透我也是蛮拼的..把1030和这道题对比了无数遍...最终结论是...无视时间复杂度,1030可以用这种写法解. ...
- 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 ...
- poj 2778 DNA Sequence AC自动机DP 矩阵优化
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11860 Accepted: 4527 Des ...
- POJ 2778 DNA Sequence ( AC自动机、Trie图、矩阵快速幂、DP )
题意 : 给出一些病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 分析 : 这题搞了我真特么久啊,首先你需要知道的前置技能包括 AC自动机.构建Trie图.矩阵快速幂,其中矩 ...
- [poj2778 DNA Sequence]AC自动机,矩阵快速幂
题意:给一些字符串的集合S和整数n,求满足 长度为n 只含charset = {'A'.'T‘.'G'.'C'}包含的字符 不包含S中任一字符串 的字符串的种类数. 思路:首先对S建立ac自动机,考虑 ...
随机推荐
- 发送Post的请求代码
通过浏览器访问的URL请求,都是GET请求,接下来代码是模拟POST发送请求 import java.io.BufferedReader; import java.io.FileNotFoundExc ...
- linux中grep注意
grep -l 只输出文件名: -h 只输出匹配的行 不输出文件名: -c 之处匹配内容的行数: -n 将结果输出的同时,也输出改行的行号: -c 统计查到的总行数: -i 忽略大小写: grep ' ...
- zookeeper客户端 和服务器连接时版本问题
在使用kafka 和zookeeper 实现实时分析程序时,由于zookeeper部署版本和分析程序导入jar包的版本不一致,导致了当实时分析程序从远程服务器连接kafka集群的zookeeper时报 ...
- HDU 5301(Buildings-贪心构造)
Buildings Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tota ...
- js:string转int
http://blog.csdn.net/leidengyan/article/details/5503594 <script> var str='1250' ; aler ...
- C#.Net调试时调无法“编辑并继续”
‘启用编辑并继续’的作用是允许用户在调试的过程中修改源代码,并且修改的代码会编译到调试进程中立刻生效. 在调试时,无法查到变量的值,也无法编辑,若编辑会跳出如下弹框: 解决方法: 1:如下图,选择De ...
- QTreeWidget 的用法
Qt QTreeWidget 新建一个Qt Widgets Application,拖拽一个Tree Widget 到 ui 界面上,最后实现的效果如下: 添加代码 //test.h //在头文件里添 ...
- asp.net core 系列之Reponse caching之cache in-memory (2)
这篇文章(主要翻译于官网,水平有限,见谅)讲解asp.net core 中的 Cache in-memory (内存缓存). Cache in-memory in ASP.NET Core Cachi ...
- CocoaPods安装教程 pod setup很慢解决方案
CocoaPods安装教程 pod setup很慢解决方案 http://www.jianshu.com/p/6230eec137f6
- cacti 主机/网络设备流量监控 图解
1.在配置中找到设备 console —> Device 2.初次添加 cacti 监控主机的时候是没有任何设备的,所以要选择add 添加你要监控的主机 \