Description

It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's very useful to analyze a segment of DNA Sequence,For example, if a animal's DNA sequence contains segment ATC then it may mean that the animal may have a genetic disease. Until now scientists have found several those segments, the problem is how many kinds of DNA sequences of a species don't contain those segments.

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

First line contains two integer m (0 <= m <= 10), n (1 <= n <=2000000000). Here, m is the number of genetic disease segment, and n is the length of sequences.

Next m lines each line contain a DNA genetic disease segment, and length of these segments is not larger than 10.

Output

An integer, the number of DNA sequences, mod 100000.

Sample Input

4 3
AT
AC
AG
AA

Sample Output

36

题目大意:给定n个病毒序列,求长度为m的序列里面不含有这n种病毒序列的有多少种。
思路:参考了https://blog.csdn.net/qq_36346262/article/details/76355416 题目是求长度为m的序列 那我们可以看成是从根节点出发往下走了m步之后到达字典树上的某一个节点,而最后求得就是从根节点走了m步之后能够到达的某个节点的种类数和,当然我们在走的时候需要避开病毒,也就是不能到达我们树中被标记过的点。最后就将这个问题转化成了求从根节点出发走m步之后到达任意一个k节点的种类数,可以用矩阵快速幂来求解。
ps:假如有一个矩阵mp[i][j]代表了从i节点走一步走到j节点的种类数,那么我们就用(mp[i][j])^n代表从i节点走n步之后到达j节点的种类数;
 #include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue> using namespace std;
typedef long long LL;
const int max_tot = ;
const int max_size = ;
const LL mod = ;
char s[];
struct mac {
LL a[][];//由于只有小于等于10个单词且每个单词长度不超过10,所以最多有100个节点
int len;
mac() {
len = ;
memset(a, , sizeof(a));
}
mac operator*(const mac &c)const {
mac t; t.len = len;
for (int i = ; i < len; i++)
for (int j = ; j < len; j++) {
t.a[i][j] = ;
for (int k = ; k < len; k++)
t.a[i][j] += a[i][k] * c.a[k][j];
t.a[i][j] %= mod;
}
return t;
}
};
mac Pow(mac a, int b){
mac ans; ans.len = a.len;
for (int i = ; i < a.len; i++)ans.a[i][i] = ;
while (b) {
if (b & )
ans = ans*a;
a = a * a;
b >>= ;
}
return ans;
}
struct AC {
int trie[max_tot][max_size];
int val[max_tot];
int fail[max_tot], last[max_tot];
int size;
void Clear()
{
memset(trie[], , sizeof(trie[]));
size = ;
}
int idx(char x) {
if (x == 'A')return ;
if (x == 'C')return ;
if (x == 'G')return ;
if (x == 'T')return ;
}
void insert(char *str) {
int k = ;
for (int i = ; str[i]; i++) {
int x = idx(str[i]);
if (!trie[k][x]) {
memset(trie[size], , sizeof(trie[size]));
val[size] = ;
trie[k][x] = size++;
}
k = trie[k][x];
}
val[k] = ;
}
void GetFail()
{
queue<int>Q;
fail[] = ; int k = ;
for (int i = ; i < max_size; i++) {//计算第一层的fail指针跟last指针
k = trie[][i];
if (k) {
Q.push(k);
fail[k] = ;
last[k] = ;
}
}
while (!Q.empty()) {
int r = Q.front(); Q.pop();
for (int i = ; i < max_size; i++) {
k = trie[r][i];
if (!k) {
trie[r][i] = trie[fail[r]][i];
val[r] = val[r] || val[fail[r]];
continue;
}
Q.push(k);
int v = fail[r];
while (v && !trie[v][i])v = fail[k];
fail[k] = trie[v][i];
last[k] = (val[fail[k]] ? fail[k] : last[fail[k]]);
}
}
}
}ac;
int main()
{
ios::sync_with_stdio(false);
int n, m;
while (cin>>n>>m) {
ac.Clear();
for (int i = ; i <= n; i++) {
cin >> s;
ac.insert(s);
}
ac.GetFail();
mac ans; ans.len = ac.size;
for (int i = ; i < ac.size; i++) {
for (int j = ; j < max_size; j++) {
int u = ac.trie[i][j];
if (!ac.val[u])ans.a[i][u]++;
}
}
ans = Pow(ans, m);
LL sum = ;
for (int i = ; i < ; i++)
sum = (sum + ans.a[][i]) % mod;
cout << sum << endl;
}
return ;
}

POJ 2778 DNA Sequence (ac自动机+矩阵快速幂)的更多相关文章

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

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

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

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

  3. POJ 2778 DNA Sequence (AC自动机,矩阵乘法)

    题意:给定n个不能出现的模式串,给定一个长度m,要求长度为m的合法串有多少种. 思路:用AC自动机,利用AC自动机上的节点做矩阵乘法. #include<iostream> #includ ...

  4. [poj2778]DNA Sequence(AC自动机+矩阵快速幂)

    题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) 解题关键:AC自动机,实际上就是一个状态转移图,注意能少取模就少取模, ...

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

  6. POJ 2778 DNA Sequence ( AC自动机、Trie图、矩阵快速幂、DP )

    题意 : 给出一些病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 分析 : 这题搞了我真特么久啊,首先你需要知道的前置技能包括 AC自动机.构建Trie图.矩阵快速幂,其中矩 ...

  7. POJ 2778 DNA Sequence (AC自动机+DP+矩阵)

    题意:给定一些串,然后让你构造出一个长度为 m 的串,并且不包含以上串,问你有多少个. 析:很明显,如果 m 小的话 ,直接可以用DP来解决,但是 m 太大了,我们可以认为是在AC自动机图中,根据离散 ...

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

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

  9. poj 2778 DNA Sequence AC自动机DP 矩阵优化

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11860   Accepted: 4527 Des ...

  10. poj 2778 DNA Sequence AC自动机

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11860   Accepted: 4527 Des ...

随机推荐

  1. jdbc框架-dbutils的简单使用

    jdbc框架-dbutils的简单使用 dbutils:是apache组织的一个工具类,jdbc的框架,更方便我们使用 使用步骤: 1.导入jar包(commons-dbutils-1.4.jar) ...

  2. oralce触发器

    n  触发器的分类 DML(insert,delete,update)触发器 DDL(create table ,create view...drop...)触发器 系统触发器(与系统相关的触发器,比 ...

  3. C89标准库函数手册

    http://zh.cppreference.com/w/c 前言 ANSI C(C89)标准库函数共有15个头文件.这15个头文件分别为: 1.<assert.h>            ...

  4. JavaScript--开关思想

    就是男默女泪的立flag! <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  5. 使用php实现单点登录实例详解

    1.首先准备两个虚拟域名 127.0.0.1 www.openpoor.com 127.0.0.1 www.myspace.com 2.在openpoor的根目录下创建以下文件 index.php文件 ...

  6. 如何mock https请求

    最近在测试项目过程当中,遇到客户端mock https请求的场景,但是默认用charles抓取出来的https请求是乱码的,对于这类请求如何来mock,有以下2种方式: 1.这里有篇http://co ...

  7. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第九章:贴图

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第九章:贴图 代码工程地址: https://github.com/j ...

  8. MyBatis动态SQL(一)

    MyBatis 的强大特性之一便是它的动态 SQL.动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似.在 MyBatis 之前的版本中,有很多元素需要花时间了解.MyBatis 3 ...

  9. pytest笔记

    -v 参数显示执行过程 测试覆盖率: ldy@ldy-D214:~/workspace/socai$ pipenv run pytest tests/unit/test_models.py --cov ...

  10. hdu 3652 【数位dp】

    hdu 3652 题意:求1到n中包含'13'('13'不一定连续)且能被13整除的数的个数. 这是我第一道比较了能看懂的数位dp.定义状态dp[pos][res][sta]表示处理到第pos位,模的 ...