POJ 3691 DNA Sequence (AC自动机 + 矩阵 有bug,待修改)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9889 | Accepted: 3712 |
Description
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
Next m lines each line contain a DNA genetic disease segment, and length of these segments is not larger than 10.
Output
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,待修改)的更多相关文章
- poj 2778 DNA Sequence ac自动机+矩阵快速幂
链接:http://poj.org/problem?id=2778 题意:给定不超过10串,每串长度不超过10的灾难基因:问在之后给定的长度不超过2e9的基因长度中不包含灾难基因的基因有多少中? DN ...
- POJ 2778 DNA Sequence (AC自动机,矩阵乘法)
题意:给定n个不能出现的模式串,给定一个长度m,要求长度为m的合法串有多少种. 思路:用AC自动机,利用AC自动机上的节点做矩阵乘法. #include<iostream> #includ ...
- poj 2778 DNA Sequence AC自动机DP 矩阵优化
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11860 Accepted: 4527 Des ...
- POJ 2778 DNA Sequence ( AC自动机、Trie图、矩阵快速幂、DP )
题意 : 给出一些病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 分析 : 这题搞了我真特么久啊,首先你需要知道的前置技能包括 AC自动机.构建Trie图.矩阵快速幂,其中矩 ...
- POJ2278 DNA Sequence —— AC自动机 + 矩阵优化
题目链接:https://vjudge.net/problem/POJ-2778 DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Tota ...
- [poj2778]DNA Sequence(AC自动机+矩阵快速幂)
题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) 解题关键:AC自动机,实际上就是一个状态转移图,注意能少取模就少取模, ...
- POJ2778 DNA Sequence(AC自动机 矩阵)
先使用AC自动机求得状态转移关系,再建立矩阵,mat[i][j]表示一步可从i到j且i,j节点均非终止字符的方案数,则此矩阵的n次方表示n步从i,到j的方法数. #include<cstdio& ...
- POJ 3691 DNA repair(AC自动机+DP)
题目链接 能AC还是很开心的...此题没有POJ2778那么难,那个题还需要矩阵乘法,两个题有点相似的. 做题之前,把2778代码重新看了一下,回忆一下当时做题的思路,回忆AC自动机是干嘛的... 状 ...
- poj 2778 DNA Sequence AC自动机
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11860 Accepted: 4527 Des ...
- 【距离GDOI:128天】【POJ2778】DNA Sequence(AC自动机+矩阵加速)
已经128天了?怎么觉得上次倒计时150天的日子还很近啊 ....好吧为了把AC自动机搞透我也是蛮拼的..把1030和这道题对比了无数遍...最终结论是...无视时间复杂度,1030可以用这种写法解. ...
随机推荐
- 启明星Exchange/outlook预定会议室终端显示解决方案
启明星会议室预定系统(Exchange2007及其以上版本,)终端调用说明 (一)技术原理 系统采用三级刷新方式,以尽可能减少对服务器的访问压力. (1) exe程序,每隔5分钟访问Exchange, ...
- iOS:删除、插入、移动单元格
删除.插入.移动单元格的具体实例如下: 代码如下: #import "ViewController.h" #define NUM 20 typedef enum { delet ...
- Strings of Power
B. Strings of Power Volodya likes listening to heavy metal and (occasionally) reading. No wonder Vol ...
- go语言基础之切片做函数参数
1.切片做函数参数 (备注:用了冒泡排序) 示例: package main //必须有个main包 import "fmt" import "math/rand&quo ...
- CentOS开启和关闭防火墙
CentOS Linux开启和关闭防火墙命令有两种,一种是临时的,重启即复原:另外一种是永久性的,重启不会复原. 1) 临时生效,重启后复原 开启: service iptables start ...
- 在Android中使用Android Ksoap2调用WebService
一.WebService介绍 WebService是基于SOAP协议可实现web服务器与web服务器之间的通信,因采用SOAP协议传送XML数据具有平台无关性,也是成为解决异构平台之间通信的重要解决方 ...
- 【Quick-COCOS2D-X 3.3 怎样绑定自己定义类至Lua之中的一个】环境搭建
* 确定你安装了Android NDK R9B 版本号 ,假设没有前往下面地址下载. ( https://dl.google.com/android/ndk/android-ndk-r9b-linux ...
- 每日一水 POJ8道水题
1. POJ 3299 Humidex 链接: http://poj.org/problem?id=3299 这道题是已知H,D,T三者的运算关系,然后告诉你其中两个.求另一个. #include&l ...
- 拼接多个 wchar_t *
/* wcscat example */ #include <wchar.h> int main () { wchar_t wcs[80]; wcscpy (wcs,L" ...
- Mac Finder中如何复制当前完整路径
1.拖到命令行 2.在Finder中command+i 会弹出详细信息,然后[位置]处进行 copy 3.利用Automator,添加一个服务的快捷键. 转自:http://q.cnblogs.com ...