poj 2778 DNA Sequence 状态及状态转移 AC自动机 矩阵快速幂
题目链接
题意
给定\(m\)个字符串,问长度为\(n\)的字符串中有多少个不包含那\(m\)个字符串。
(字符集为\(A,T,C,G\),\(m\leq 10\),长度\(\leq 10\),\(n\leq 2e9\))
思路
状态转移——矩阵
构造一个矩阵\(m[\ ][\ ]\),\(m[i][j]\)代表有多少种方式可以走一步从第\(i\)个节点到第\(j\)个节点,
则\(m^n[i][j]\)即代表有多少种方式可以走\(n\)步从第\(i\)个节点到第\(j\)个节点,
于是答案呼之欲出——\(\sum_{i=0}^{cnt}m^n[0][i]\),即从根节点走到其他所有节点的方式数总和。
状态——AC自动机
且慢,上面说的节点是什么?
——是\(AC\)自动机上的状态节点(AC自动机本质上是状态机)。
那么走一步又是怎么体现的?
——从\(x\)走到\(son[x][i]\)即为走一步。
不能包含的状态又是怎么处理呢?
——将所有能够通过fail指针走到某个单词结尾状态的节点全都排除,这一点由后缀关系易见。
Code
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <queue>
#define SIZE 4
#define maxn 110
using namespace std;
typedef long long LL;
char s[12];
int son[maxn][4], fail[maxn], flag[maxn], mp[maxn], mp2[maxn], tot, mat[maxn][maxn];
void add(char* s, int len) {
int p = 0;
for (int i = 0; i < len; ++i) {
if (son[p][mp[s[i]]] == -1) {
flag[++tot] = 0;
for (int j = 0; j < SIZE; ++j) son[tot][j] = -1;
son[p][mp[s[i]]] = tot;
}
p = son[p][mp[s[i]]];
}
flag[p] = 1;
}
void build() {
queue<int> que;
fail[0] = 0;
for (int i = 0; i < SIZE; ++i) {
if (son[0][i] == -1) son[0][i] = 0;
else {
fail[son[0][i]] = 0;
que.push(son[0][i]);
}
++mat[0][son[0][i]];
}
while (!que.empty()) {
int x = que.front(); que.pop();
for (int i = 0; i < SIZE; ++i) {
if (son[x][i] == -1) son[x][i] = son[fail[x]][i];
else {
fail[son[x][i]] = son[fail[x]][i];
flag[son[x][i]] |= flag[fail[son[x][i]]];
que.push(son[x][i]);
}
++mat[x][son[x][i]];
}
}
}
void init() {
mp['A'] = 0, mp['T'] = 1, mp['C'] = 2, mp['G'] = 3;
flag[tot = 0] = 0;
for (int i = 0; i < SIZE; ++i) son[0][i] = -1;
}
int cnt;
const LL mod = 100000;
typedef struct {
LL mat[maxn][maxn];
void init(LL x){
memset(mat, 0, sizeof(mat));
for(int i=0; i<cnt; i++) mat[i][i] = x;
}
} Matrix;
Matrix m0;
LL addl(LL a, LL b) { return (a + b + mod) % mod; }
LL mull(LL a, LL b) { return a * b % mod; }
Matrix mulm(const Matrix& a, const Matrix& b) {
Matrix temp;
temp.init(0);
for (int i = 0; i < cnt; ++i) {
for (int j = 0; j < cnt; ++j) {
for (int k = 0; k < cnt; ++k) temp.mat[i][j] = addl(temp.mat[i][j], mull(a.mat[i][k], b.mat[k][j]));
}
}
return temp;
}
Matrix poww(LL n) {
Matrix a = m0, ret;
ret.init(1);
while (n) {
if (n & 1) ret = mulm(ret, a);
a = mulm(a, a);
n >>= 1;
}
return ret;
}
int main() {
int m; LL n;
scanf("%d%lld", &m, &n);
init();
for (int i = 0; i < m; ++i) {
scanf("%s", &s);
add(s, strlen(s));
}
build();
cnt=0;
for (int i = 0; i <= tot; ++i) if (!flag[i]) mp2[cnt++] = i;
for (int i = 0; i < cnt; ++i) {
for (int j = 0; j < cnt; ++j) {
m0.mat[i][j] = mat[mp2[i]][mp2[j]];
}
}
Matrix fnl = poww(n);
LL ans=0;
for (int i = 0; i < cnt; ++i) ans = addl(ans, fnl.mat[0][i]);
printf("%lld\n", ans);
return 0;
}
poj 2778 DNA Sequence 状态及状态转移 AC自动机 矩阵快速幂的更多相关文章
- 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 ...
- POJ2778 DNA Sequence(AC自动机+矩阵快速幂)
题目给m个病毒串,问不包含病毒串的长度n的DNA片段有几个. 感觉这题好神,看了好久的题解. 所有病毒串构造一个AC自动机,这个AC自动机可以看作一张有向图,图上的每个顶点就是Trie树上的结点,每个 ...
- poj 2778 AC自动机+矩阵快速幂
题目链接:https://vjudge.net/problem/POJ-2778 题意:输入n和m表示n个病毒,和一个长为m的字符串,里面只可以有'A','C','G','T' 这四个字符,现在问这个 ...
- POJ - 2778 ~ HDU - 2243 AC自动机+矩阵快速幂
这两题属于AC自动机的第二种套路通过矩阵快速幂求方案数. 题意:给m个病毒字符串,问长度为n的DNA片段有多少种没有包含病毒串的. 根据AC自动机的tire图,我们可以获得一个可达矩阵. 关于这题的t ...
- poj2778DNA Sequence (AC自动机+矩阵快速幂)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud DNA Sequence Time Limit: 1000MS Memory ...
- POJ 2778 DNA Sequence(AC自动机+矩阵快速幂)
题目链接:http://poj.org/problem?id=2778 题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) ...
- 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 ...
随机推荐
- LAMP 一键部署
LAMP 一键部署 部署http #!/bin/bash ### global variables export lamp_repo=http://192.168.1.5/lamp/ export l ...
- LeetCode949-给定数字能组成的最大时间
问题: 给定一个由 4 位数字组成的数组,返回可以设置的符合 24 小时制的最大时间. 最小的 24 小时制时间是 00:00,而最大的是 23:59.从 00:00 (午夜)开始算起,过得越久,时间 ...
- PHP获取接下来一周的日期
//获取接下来一周的日期 function GetWeeks() { $i=0; $weeks=[]; for ($i;$i<=7;$i++){ $month=date('m',time()+8 ...
- Android Studio 3.0 安装注意点
在安装Android studio 3.0+ 时候,会遇到默认不带Android SDK 的问题. 在启动Android studio 后,会提示让选择SDK目录,选择下载目录,对应的去下载 那么问题 ...
- JZOJ 3382. 【NOIP2013模拟】七夕祭
3382. [NOIP2013模拟]七夕祭 Time Limits: 1000 ms Memory Limits: 131072 KB Detailed Limits Goto Problem ...
- P2590 [ZJOI2008]树的统计(LCT)
P2590 [ZJOI2008]树的统计 题目描述 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把 ...
- Nodejs-内置核心模块&npm包管理工具
1.核心模块的意义 如果只是在服务器运行JavaScript代码,其实意义不大(浏览器就可以解决)因为无法实现功能(读写文件,访问网络) Node的用处在于本身还提供了一系列的功能模块,用于与操作系统 ...
- asp.net中使用ffmpeg
protected void Button1_Click(object sender, EventArgs e) { string FFmpegArguments = @" -i D:\离歌 ...
- Careercup - Microsoft面试题 - 4639756264669184
2014-05-12 06:42 题目链接 原题: Write your own regular expression parser for following condition: az*b can ...
- IOS开发学习笔记038-autolayout 自动布局 界面实现
在storyboard/xib文件中实现自动布局 autolayout 1.注意事项 autolayout和frame属性是有冲突的,所以如果准备使用autolayout,就不要再代码中对控件的fra ...