DNA Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9889   Accepted: 3712

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

 
 
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; const int N=;
const int mod=; struct Trie{
int ok;
int fail;
int next[];
void Init(){
ok=;
fail=-;
memset(next,-,sizeof(next));
}
}a[N]; char wrd[];
char str[N];
int n,m,cnt,q[N]; int find(char ch){
switch(ch){
case 'A':return ;
case 'C':return ;
case 'T':return ;
case 'G':return ;
}
return ;
} void InsertTrie(char *str){
int p=;
for(int i=;str[i]!='\0';i++){
int id=find(str[i]);
if(a[p].ok)
break;
if(a[p].next[id]==-){
a[p].next[id]=cnt++;
a[cnt-].Init();
}
p=a[p].next[id];
}
a[p].ok++;
} void AC_automation(){
int head=,tail=;
q[tail++]=;
int cur=,tmp;
while(head<tail){
cur=q[head++];
for(int i=;i<;i++){
tmp=a[cur].next[i];
if(tmp!=-){
if(cur==)
a[tmp].fail=;
else{
a[tmp].fail=a[a[cur].fail].next[i];
if(a[a[tmp].fail].ok)
a[tmp].ok++;
}
q[tail++]=a[cur].next[i];
}else{
if(cur==)
a[cur].next[i]=;
else
a[cur].next[i]=a[a[cur].fail].next[i];
}
}
}
} struct Matrix{
long long m[][];
}; Matrix init,unit; void Init(){
memset(init.m,,sizeof(init.m));
for(int i=;i<cnt;i++)
if(!a[i].ok)
for(int j=;j<;j++){
if(a[a[i].next[j]].ok==)
init.m[i][a[i].next[j]]++;
}
//if(a[*a[i].next[j]].ok==0)
//init.m[i][*a[i].next[j]]++;
for(int i=;i<cnt;i++)
for(int j=;j<cnt;j++)
unit.m[i][j]=(i==j)?:;
} Matrix Mul(Matrix a,Matrix b){
Matrix c;
for(int i=;i<cnt;i++)
for(int j=;j<cnt;j++){
c.m[i][j]=;
for(int k=;k<cnt;k++)
c.m[i][j]=(a.m[i][k]*b.m[k][j])%mod;
c.m[i][j]%=mod;
}
return c;
} Matrix Pow(Matrix a,Matrix b,int k){
while(k){
if(k&){
b=Mul(a,b);
}
a=Mul(a,a);
k>>=;
}
return b;
} int main(){ freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){
cnt=;
a[].Init();
for(int i=;i<n;i++){
scanf("%s",wrd);
InsertTrie(wrd);
}
AC_automation();
Init();
Matrix res=Pow(init,unit,m);
long long ans=;
for(int i=;i<cnt;i++)
if(a[i].ok==)
ans=(ans+res.m[][i])%mod;
cout<<ans<<endl;
}
return ;
}

POJ 3691 DNA Sequence (AC自动机 + 矩阵 有bug,待修改)的更多相关文章

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

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

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

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

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

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

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

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

  5. POJ2278 DNA Sequence —— AC自动机 + 矩阵优化

    题目链接:https://vjudge.net/problem/POJ-2778 DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Tota ...

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

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

  7. POJ2778 DNA Sequence(AC自动机 矩阵)

    先使用AC自动机求得状态转移关系,再建立矩阵,mat[i][j]表示一步可从i到j且i,j节点均非终止字符的方案数,则此矩阵的n次方表示n步从i,到j的方法数. #include<cstdio& ...

  8. POJ 3691 DNA repair(AC自动机+DP)

    题目链接 能AC还是很开心的...此题没有POJ2778那么难,那个题还需要矩阵乘法,两个题有点相似的. 做题之前,把2778代码重新看了一下,回忆一下当时做题的思路,回忆AC自动机是干嘛的... 状 ...

  9. poj 2778 DNA Sequence AC自动机

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

  10. 【距离GDOI:128天】【POJ2778】DNA Sequence(AC自动机+矩阵加速)

    已经128天了?怎么觉得上次倒计时150天的日子还很近啊 ....好吧为了把AC自动机搞透我也是蛮拼的..把1030和这道题对比了无数遍...最终结论是...无视时间复杂度,1030可以用这种写法解. ...

随机推荐

  1. iOS:UIToolBar、toolbarItems、BarButtonItem的几种关系

    工具栏:ToolBar 工具栏项目:Bar Button Item 调节按钮位置的固定调节:Fixed Space Bar Button Item 调节按钮位置的灵活调节:Flexible Space ...

  2. WIDGET和鼠标特效的DEMO

    原创WIDGET和鼠标特效的DEMO, 键盘1 平移Widget键盘2 旋转Widget键盘3 缩放Widget DEMO中,实现对BOX的旋转缩放位移 下载地址: http://pan.baidu. ...

  3. Linked List Cycle leetcode II java (寻找链表环的入口)

    题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...

  4. IDA 远程调试 Android so

      1.把ida 目录下android_server 传到android 目录中如:adb push  android_server /data/local/tmp/adb shell 进入模拟器cd ...

  5. 【转】NativeScript的工作原理:用JavaScript调用原生API实现跨平台

    原文:https://blog.csdn.net/qq_21298703/article/details/44982547 -------------------------------------- ...

  6. 正则 js截取时间

    项目中要把时间截取,只要年月日,不要时分秒,于是 /\d{4}-\d{1,2}-\d{1,2}/g.exec("2012-6-18 00:00:00") 或者另一种 var dat ...

  7. MySQL数据源在Spring中的配置

    干脆把MySQL的数据源配置也一起放这里,以备不时之需. MySQL的驱动包可以从这里 http://pan.baidu.com/s/1d02aZ 下载. 以下粗体部分是需要你根据实际情况调整的. & ...

  8. HTTP请求格式和HTTP响应格式

    主要内容: 1.HTTP请求格式 2.HTTP响应格式 一.HTTP请求格式 当浏览器向Web服务器发出请求时,它向服务器传递了一个数据块,也就是请求信息,HTTP请求信息由3部分组成:l   请求方 ...

  9. 通过HTML5 Visibility API检测页面活动状态

    几年前,我们浏览网页的时候是没有选项卡浏览模式的,每一个网页都会是一个浏览器窗口,如果我没有记错,Win7之前我们都是这样浏览网页的.作为一个程序员,我们经常会同时打开10-15个网页,多的时候超过2 ...

  10. iptables与SELinux

    iptables: 开始配置我们来配置一个filter表的防火墙.(1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables -L Chain INPUT (policy ...