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. 装了ubuntu之后,只能进入ubuntu系统,不能进入windows系统

    电脑之前安装的是Windows 7系统, 后来在安装Linux系统中(快要完成)出现了故障, 没办法只能关机,之后重启,重启后只能进入Linux系统了 解决方案: 使用sudo update-grub ...

  2. 【To Read】Shortest Palindrome(KMP)

    题意:Given a string S, you are allowed to convert it to a palindrome by adding characters in front of ...

  3. 散列表(Hash Table)

    散列表(hash table): 也称为哈希表. 根据wikipedia的定义:是根据关键字(Key value)而直接访问在内存存储位置的数据结构.也就是说,它通过把键值通过一个函数的计算,映射到表 ...

  4. sqlplus连接数据库报错SP2-0642: SQL*Plus internal error state 2130, context 0:0:0解决

    sqlplus连接数据库报错SP2-0642: SQL*Plus internal error state 2130, context 0:0:0解决 sqlplus 连接数据库报错SP2-0642: ...

  5. 2019-7-20-win10-uwp-使用-msbuild-命令行编译-UWP-程序

    title author date CreateTime categories win10 uwp 使用 msbuild 命令行编译 UWP 程序 lindexi 2019-07-20 21:56:2 ...

  6. AIDL基本用法

    1. AIDL有什么用?用TA到目的是什么? 2. 怎么用AIDL? 1. AIDL有什么用? 1.1. 为了提高代码执行速度,将部分逻辑封入C/C++代码中  1.2. 为了调用这部分代码,使用JN ...

  7. Java8 Date与LocalDate互转

    Java8 日期时间API,新增了LocalDate.LocalDateTime.LocalTime等线程安全类,接下来要说的是LocalDate与java.util.Date之间的转换. 1.Loc ...

  8. oracle函数 NLS_INITCAP(x[,y])

    [功能]返回字符串并将字符串的第一个字母变为大写,其它字母小写; [参数]x字符型表达式 [参数]Nls_param可选, 查询数据级的NLS设置:select * from nls_database ...

  9. Best Open Source Software

    Best Open Source Software Open Source, Software, Top The promise of open source software is best qua ...

  10. ODT 珂朵莉树 入门

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...