题目链接

题意

给定\(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. 二十一、MySQL NULL 值处理

    MySQL NULL 值处理 我们已经知道 MySQL 使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作. ...

  2. 七、MySQL 选择数据库

    MySQL 选择数据库 在你连接到 MySQL 数据库后,可能有多个可以操作的数据库,所以你需要选择你要操作的数据库. 从命令提示窗口中选择MySQL数据库 在 mysql> 提示窗口中可以很简 ...

  3. 通用后台管理系统源码,响应式布局,Java管理系统源码,零门槛安装部署

    本项目是一个通用响应式管理后台,导入开发环境安装就能直接运行,界面也非诚漂亮,在PC端和移动端也是自适应的.非常适合企业或者个人搭建各种商城后台,博客后台,网站管理后台等. 源码启动后的截图 需要这套 ...

  4. JavaScript对象创建的九种方式

    1.标准创建对象模式 var person = new Object(); person.name = "Nicholas"; person.age = 29; person.jo ...

  5. python练手习题

    不断记录python常见习题,不断寻求更多更好的解决办法.持续更新中..... 练习: 1. list两两元素交换位置,如[1,2,3,4,5,6] 执行后为 -> [2,1,4,3,6,5] ...

  6. Postgres安装详解

    PG安装 一.基础包的安装(yum源的配置,可以采用光盘挂载,及ftp yum源,针对外网环境忽略此步): yum -y install wget tcpdump glibc libgcc gcc g ...

  7. (PowerDesigner&Sqlite)PD中设计完表后,将其导入数据库中

    本人连接过SQLServer跟SQLite Ⅰ.SQLServer,百度,转一下:http://jingyan.baidu.com/article/7f766daf465e9c4101e1d0d5.h ...

  8. P3116 [USACO15JAN]会议时间Meeting Time

    P3116 [USACO15JAN]会议时间Meeting Time 题目描述 Bessie and her sister Elsie want to travel from the barn to ...

  9. 线段树[To be continued]

    目录 数据结构--线段树 一.定义 二.性质 三.基本操作 0.结构体 1.建树 2.单点查询 3.单点修改 4.区间修改 5.区间查询 四.题目 单点修改.区域查询模板 五.鸣谢 学姐的Blog 百 ...

  10. 小知识(h5 js )

    1.如果都为NaN但是他们不相等var a=NaN;var b=NaN;a==b //flase2.javascript 是一种脚本语言,可以创建服务器端和客户端的脚本3.javascript 中有两 ...