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. Elasticsearch重要文章之四:监控每个节点(ThreadPool部分)

    http://zhaoyanblog.com/archives/754.html ThreadPool部分 Elasticsearch 内部使用了线程池,通过这些线程池之间的合作完成工作,在需要时传递 ...

  2. typescript-koa-postgresql 实现一个简单的rest风格服务器 —— 连接 postgresql 数据库

    接上一篇,这里使用 sequelize 来连接 postgresql 数据库 1.安装 sequelize,数据库驱动 pg yarn add sequelize sequelize-typescri ...

  3. [每天解决一问题系列 - 0002] Xcopy cannot copy file with long directory

    现象: 当xcopy的文件的全名(包括目录和文件名)的长度超过255字符时,会copy失败,得到insufficient memory错误 解决方法: 在Server 版的OS中,有robcopy命令 ...

  4. (转)Python开发程序:支持多用户在线的FTP程序

    原文链接:http://www.itnose.net/detail/6642756.html 作业:开发一个支持多用户在线的FTP程序 要求: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ...

  5. django中url,静态文件,POST请求的配置 分类: Python 2015-06-01 17:00 789人阅读 评论(0) 收藏

    平时使用的是pycharm,所以这篇文章主要也是使用pycharm默认创建的django项目为基础进行讲解.项目目录如下图: 1.URL的配置 当创建好项目后,运行项目就可以看到django默认的页面 ...

  6. 源码分析篇 - Android绘制流程(一)窗口启动流程分析

    Activity.View.Window之间的关系可以用以下的简要UML关系图表示,在这里贴出来,比较能够帮组后面流程分析部分的阅读. 一.Activity的启动流程 在startActivity() ...

  7. Error:fatal: Not a git repository (or any of the parent directories): .git

    在项目目录下执行git init命令. 大功告成.

  8. Is it possible to display icons in a PopupMenu?

    I really like the new PopupMenu we got in 3.0, but I just can't display any icons next to the menu i ...

  9. Linux系统基础知识整理

    一.说明 本篇文章,我将结合自己的实践以及简介,来对linux系统做一个直观清晰的介绍,使得哪些刚接触Linux的小伙伴可以快速入门,也方便自己以后进行复习查阅. 二.基本知识整理 1.Linux文件 ...

  10. PHP多进程系列笔记(一)

    本系列文章将向大家讲解pcntl_*系列函数,从而更深入的理解进程相关知识. PCNTL在PHP中进程控制支持默认是关闭的.您需要使用 --enable-pcntl 配置选项重新编译PHP的 CGI或 ...