题目链接

题意

给定\(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自动机 矩阵快速幂的更多相关文章

  1. 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 ...

  2. 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 ...

  3. POJ2778 DNA Sequence(AC自动机+矩阵快速幂)

    题目给m个病毒串,问不包含病毒串的长度n的DNA片段有几个. 感觉这题好神,看了好久的题解. 所有病毒串构造一个AC自动机,这个AC自动机可以看作一张有向图,图上的每个顶点就是Trie树上的结点,每个 ...

  4. poj 2778 AC自动机+矩阵快速幂

    题目链接:https://vjudge.net/problem/POJ-2778 题意:输入n和m表示n个病毒,和一个长为m的字符串,里面只可以有'A','C','G','T' 这四个字符,现在问这个 ...

  5. POJ - 2778 ~ HDU - 2243 AC自动机+矩阵快速幂

    这两题属于AC自动机的第二种套路通过矩阵快速幂求方案数. 题意:给m个病毒字符串,问长度为n的DNA片段有多少种没有包含病毒串的. 根据AC自动机的tire图,我们可以获得一个可达矩阵. 关于这题的t ...

  6. poj2778DNA Sequence (AC自动机+矩阵快速幂)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud DNA Sequence Time Limit: 1000MS   Memory ...

  7. POJ 2778 DNA Sequence(AC自动机+矩阵快速幂)

    题目链接:http://poj.org/problem?id=2778 题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) ...

  8. poj 2778 DNA Sequence ac自动机+矩阵快速幂

    链接:http://poj.org/problem?id=2778 题意:给定不超过10串,每串长度不超过10的灾难基因:问在之后给定的长度不超过2e9的基因长度中不包含灾难基因的基因有多少中? DN ...

  9. 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 ...

随机推荐

  1. JS的四舍五入问题

    最近踩了一个坑,mark一下toFixed四舍五入问题,详见代码: var myFixed = function(num, fix) { num = (parseFloat(num) * + ) / ...

  2. python面试题之介绍一下Python中webbrowser的用法

    所属网站分类: 面试经典 > python 作者:外星人入侵 链接: http://www.pythonheidong.com/blog/article/13/ 来源:python黑洞网 www ...

  3. mysql-show processlist之writing to net

    mysql提示Writing to net解决 最近发现某一个数据库cpu占用比较过.超过200%了. 首先查看数据库慢日志,设定慢日志5秒,基本上没有产生日,没有超过5秒的语句. show proc ...

  4. Python linecache模块

    Table of Contents 1. linecache 1.1. 其它 2. 参考资料 linecache 今天分享一个python的小模块: linecache, 可以用它方便地获取某一文件某 ...

  5. mongoTemplate学习笔记

    mongoTemplate的andExpression表达式 Aggregation<Post> agg = Aggregation.newAggregation( Record.clas ...

  6. loj2091 「ZJOI2016」小星星

    ref 总的来说,就是 容斥转化为点对应到点集问题. 树形 dp 解决转化后的问题. #include <iostream> #include <cstring> #inclu ...

  7. 分分钟教你做出自己的新闻阅读APP

    分分钟教你做出自己的新闻阅读APP 引子 曾经不小心发现了一些好的看新闻的网站,但是电脑又不是随身携带,因此想要下载一个这个网站的手机APP来看新闻,但是问题来了,这个网站根本没有做Android端! ...

  8. 使用bat命令实现拖动快速安装APK包

    平时安装APK包,每次都要打命令adb install *********** 很繁琐,网上找到一个用BAT命令快速安装的方法   在桌面创建一个bat文件,输入: @echo off title i ...

  9. Python-S9-Day101 Vue-cli

    01 昨天内容回顾 02 音乐播放器计算属性方法和组件创建 03 Vue-cli项目生成 04 模板中组件的使用 01 昨天内容回顾 1.1 {{xxx}}模板语法,插值,简单的运算: 1.2 指令系 ...

  10. AlloyClip的简单使用

    <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8&quo ...