DNA Sequence

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17160   Accepted: 6616

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

Source

 
 //2017-08-10
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define ll long long using namespace std; const int K = ;
const int N = ;
const int M = ;
const int MOD = ; struct Matrix{
ll a[M*M][M*M];
int r, c;
}mat, tmp; Matrix multi(Matrix x, Matrix y)//矩阵乘法
{
Matrix z;
memset(z.a, , sizeof(z.a));
z.r = x.r, z.c = y.c;
for(int i = ; i < x.r; i++){
for(int k = ; k < x.c; k++)//加速优化
{
if(x.a[i][k] == ) continue;
for(int j = ; j< y.c; j++)
z.a[i][j] = (z.a[i][j] + (x.a[i][k] * y.a[k][j]) % MOD) % MOD;
}
}
return z;
} void Matrix_pow(int n)//矩阵快速幂
{
Matrix tmp;
tmp.c = mat.c;
tmp.r = mat.r;
memset(tmp.a, , sizeof(tmp.a));
for(int i = ; i < tmp.c; i++)
tmp.a[i][i] = ;
while(n){
if(n & )
tmp = multi(tmp, mat);
mat = multi(mat, mat);
n >>= ;
}
int ans = ;
for(int i = ; i < tmp.c; i++)
ans = (ans + tmp.a[][i]) % MOD;
printf("%d\n", ans);
} struct AC_automation
{
//node nodes[N], *root, *superRoot, *cur;
int nex[M*M][], fail[M*M], match[M*M];
int root, CNT;
int newNode(){
for(int i = ; i < K; i++)
nex[CNT][i] = -;
match[CNT++] = ;
return CNT-;
}
int Hash(char ch)
{
if(ch == 'A')return ;
else if(ch == 'C')return ;
else if(ch == 'T')return ;
else if(ch == 'G')return ;
}
void init(){
CNT = ;
root = newNode();
}
void Insert(char s[]){//向字典树中插入一个字符串
int n = strlen(s);
int cur = root;
for(int i = ; i < n; i++){
int p = Hash(s[i]);
if(nex[cur][p] == -)
nex[cur][p] = newNode();
cur = nex[cur][p];
}
match[cur]++;
}
void build(){//构建自动机
queue<int> que;
fail[root] = root;
for(int i = ; i < K; i++){
if(nex[root][i] == -)
nex[root][i] = root;
else{
fail[nex[root][i]] = root;
que.push(nex[root][i]);
}
}
while(!que.empty()){
int cur = que.front();
if(match[fail[cur]])match[cur] = ;
que.pop();
for(int i = ; i < K; i++){
if(nex[cur][i] == -){
nex[cur][i] = nex[fail[cur]][i];
}else{
fail[nex[cur][i]] = nex[fail[cur]][i];
que.push(nex[cur][i]);
}
}
}
}
void to_marix(){
memset(mat.a, , sizeof(mat.a));
mat.r = mat.c = CNT;
for(int i = ; i < CNT; i++){
for(int j = ; j < ; j++)
if(!match[nex[i][j]])
mat.a[i][nex[i][j]]++;
}
// for(int i = 0; i < CNT; i++){
// for(int j = 0; j < CNT; j++)
// cout<<mat.a[i][j]<<" ";
// cout<<endl;
// }
}
}; char str[M];
AC_automation ac; int main()
{
int n, m;
while(scanf("%d%d", &m, &n)!=EOF)
{
ac.init();
for(int i = ; i < m; i++){
scanf("%s", str);
ac.Insert(str);
}
ac.build();
ac.to_marix();
Matrix_pow(n);
} return ;
}

POJ2778(SummerTrainingDay10-B AC自动机+矩阵快速幂)的更多相关文章

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

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

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

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

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

  4. poj2778 ac自动机+矩阵快速幂

    给m个子串,求长度为n的不包含子串的母串数,最直接的应该是暴搜,肯定tle,考虑用ac自动机 将子串建成字典树,通过next表来构造矩阵,然后用矩阵快速幂求长度为n的数量 邻接矩阵https://we ...

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

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

  6. HDU 2243考研路茫茫——单词情结 (AC自动机+矩阵快速幂)

    背单词,始终是复习英语的重要环节.在荒废了3年大学生涯后,Lele也终于要开始背单词了. 一天,Lele在某本单词书上看到了一个根据词根来背单词的方法.比如"ab",放在单词前一般 ...

  7. HDU 2243 考研路茫茫――单词情结 ——(AC自动机+矩阵快速幂)

    和前几天做的AC自动机类似. 思路简单但是代码200余行.. 假设solve_sub(i)表示长度为i的不含危险单词的总数. 最终答案为用总数(26^1+26^2+...+26^n)减去(solve_ ...

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

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

  9. 考研路茫茫——单词情结 HDU - 2243 AC自动机 && 矩阵快速幂

    背单词,始终是复习英语的重要环节.在荒废了3年大学生涯后,Lele也终于要开始背单词了. 一天,Lele在某本单词书上看到了一个根据词根来背单词的方法.比如"ab",放在单词前一般 ...

随机推荐

  1. Centos7调整swap分区

    如何让服务器响应更快?如何避免应用出现内存不足的错误?最简单的方法就是增加交换空间.Swap是存储盘上的一块自留地,操作系统可以在这里暂存一些内存里放不下的东西. 这从某种程度上相当于增加了服务器的可 ...

  2. Redis---SDS(简单动态字符串)

    Redis 没有直接使用 C 语言传统的字符串表示(以空字符结尾的字符数组,以下简称 C 字符串), 而是自己构建了一种名为简单动态字符串(simple dynamic string,SDS)的抽象类 ...

  3. java爬虫学习

    一.java爬取数据 示例:爬取网站中的所有古风网名:http://www.oicq88.com/gufeng/,并储存入数据库(mysql) jdk版本:jdk1.8 编辑器:idea 项目构建:m ...

  4. 创建python3虚拟环境指令和冻结所安装的包

    mkvirtualenv file_name -p python3 去掉后面的 P 和python3 代表创建python2环境 其中p代表路径的意思 冻结所安装包命令 pip freeze > ...

  5. 线程&线程控制

    线程基本概念: 1 线程 (1)概念:linux下没有真正的线程,所谓的线程都是通过进程的pcb模拟的,因此linux下的线程也称为“轻量级进程”,之前我们所说的进程现在看来,可以理解为:只有一个线程 ...

  6. [Leetcode]44.跳跃游戏Ⅰ&&45.跳跃游戏Ⅱ

    跳跃游戏链接 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出 ...

  7. Centos 7 快速搭建IOS可用IPsec

    安装 strongswan yum install -y http://ftp.nluug.nl/pub/os/Linux/distr/fedora-epel/7/x86_64/Packages/e/ ...

  8. Spark集群测试

    1. Spark Shell测试 Spark Shell是一个特别适合快速开发Spark原型程序的工具,可以帮助我们熟悉Scala语言.即使你对Scala不熟悉,仍然可以使用这一工具.Spark Sh ...

  9. C#:VS2010 由于缺少调试目标"xx.exe",Visual Studio无法开始调试,请生成项目并重试,或者相应地设置OutputPath和AssemblyName属性,使其指向目标程序集的正确位置

    解决办法:重置VS2010的环境配置 原文地址:曾是土木人 转载请注明出处:http://www.cnblogs.com/hongfei/p/3813369.html

  10. 全网最详细的Sublime Text 3的设置字体及字体大小(图文详解)

    不多说,直接上干货! 前期博客 全网最详细的Windows里下载与安装Sublime Text *(图文详解) 全网最详细的Sublime Text 3的激活(图文详解) 你也许是如下的版本:   点 ...