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. ie8 下margin-top失效的小案例

    一个小案例,是关于IE8下的margin-top的失效问题,巨日代码如下: 正常的chrome浏览器下的显示如下: margin-top=10px,正常显示 但是在ie8下,最终样式如下: margi ...

  2. python中sort排序

    排序并且改变自身结果: nums.sort()

  3. poj3422 最小费用流

    一遍的话秩序要dp就好,但是这里要删去点.此题可以转化为最小费用流.开始我想了半天纠结怎么处理到过一次后值变0,看了书之后发现拆点解决了这个问题. 对于点t,拆为t-->t',容量为1,费用为负 ...

  4. python 类的创建

  5. PHPCMS快速建站系列之邮箱验证

    1. 登录163邮箱,->设置,开启POP3服务->把SMTP服务器地址复制到PHPCMS后台. 2.开启客户端授权密码 3.填写相关信息,.可以在测试邮箱填入邮箱地址测试

  6. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第六章:在Direct3D中绘制

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第六章:在Direct3D中绘制 代码工程地址: https://gi ...

  7. Jsp中解决session过期跳转到登陆页面并跳出iframe框架的方法

    1.可以用javaScript解决在你想控制跳转的页面,比如login.jsp中的<head>与</head>之间加入以下代码: <script language=”Ja ...

  8. JQ取消hover事件

    $('a').unbind('mouseenter').unbind('mouseleave');

  9. 【NS2】ns2 otcl与c++关联(转载)

    最近几天,对ns2进行研究,ns2为什么要使用两种语言,因为C++执行速度快,因此对于一些不需要经常改变的东西:例如包的发送.而对于需要经常进行修改的就不能够使用C++,而使用OTcl脚本语言.所有O ...

  10. Mysql——ERROR 1045 (28000): Access denied for user 'root'@'localhost'

    1.修改my.ini,最后一行添加  skip-grant-tables  ,保存关闭.(win10默认安装路径:C:\ProgramData\MySQL\MySQL Server 5.7) 2.重启 ...