poj2778DNA Sequence (AC自动机+矩阵快速幂)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Time Limit: 1000MS | Memory Limit: 65536K |
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
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <queue> 5 using namespace std; 6 #define REP(A,X) for(int A=0;A<X;A++) 7 #define MAXN 100010 8 9 int p[MAXN][4]; 10 int tail[MAXN]; 11 int fail[MAXN]; 12 int root,tot; 13 const long long MOD =100000; 14 struct Matrix{ 15 int n; 16 int mat[110][110]; 17 Matrix(){} 18 Matrix(int _n){ 19 n=_n; 20 REP(i,n) 21 REP(j,n)mat[i][j]=0; 22 } 23 void init() 24 { 25 REP(i,tot) 26 REP(j,tot)mat[i][j]=0; 27 } 28 void unit() 29 { 30 REP(i,tot) 31 REP(j,tot)mat[i][j]=i==j?1:0; 32 } 33 Matrix operator *(const Matrix &a)const { 34 Matrix ret(n); 35 REP(i,n) 36 REP(j,n) 37 REP(k,n) 38 { 39 int tmp=(long long)mat[i][k]*a.mat[k][j]%MOD; 40 ret.mat[i][j]=(ret.mat[i][j]+tmp)%MOD; 41 } 42 return ret; 43 } 44 }; 45 int newnode() 46 { 47 REP(i,4)p[tot][i]=-1; 48 tail[tot++]=0; 49 return tot-1; 50 } 51 void init() 52 { 53 tot=0; 54 root=newnode(); 55 } 56 int a[MAXN]; 57 void insert(char *s){ 58 int len=strlen(s); 59 REP(i,len) 60 { 61 if(s[i]=='A')a[i]=0; 62 else if(s[i]=='C')a[i]=1; 63 else if(s[i]=='G')a[i]=2; 64 else if(s[i]=='T')a[i]=3; 65 } 66 int now= root ; 67 REP(i,len) 68 { 69 if(p[now][a[i]]==-1)p[now][a[i]]=newnode(); 70 now=p[now][a[i]]; 71 } 72 tail[now]++; 73 } 74 void build() 75 { 76 int now=root; 77 queue<int >q; 78 fail[root]=root; 79 REP(i,4){ 80 if(p[root][i]==-1){ 81 p[root][i]=root; 82 } 83 else { 84 fail[p[root][i]]=root; 85 q.push(p[root][i]); 86 } 87 } 88 while(!q.empty()) 89 { 90 now =q.front(); 91 q.pop(); 92 if(tail[fail[now]])tail[now]=1; 93 REP(i,4){ 94 if(p[now][i]==-1){ 95 p[now][i]=p[fail[now]][i]; 96 }else{ 97 fail[p[now][i]]=p[fail[now]][i]; 98 q.push(p[now][i]); 99 } 100 } 101 } 102 } 103 char s[MAXN]; 104 Matrix Mat; 105 int main() 106 { 107 ios::sync_with_stdio(false); 108 int n,m; 109 while(cin>>m>>n){ 110 init(); 111 REP(i,m){ 112 cin>>s; 113 insert(s); 114 } 115 build(); 116 Mat.n=tot; 117 Mat.init(); 118 REP(i,tot){ 119 REP(j,4){ 120 if(!tail[p[i][j]])Mat.mat[i][p[i][j]]++; 121 } 122 } 123 Matrix tmp(tot); 124 tmp.unit(); 125 while(n){ 126 if(n&1)tmp=tmp*Mat; 127 Mat=Mat*Mat; 128 n>>=1; 129 } 130 int ans=0; 131 REP(i,tot)ans+=tmp.mat[0][i]; 132 ans%=MOD; 133 cout<<ans<<endl; 134 135 } 136 return 0; 137 }
代码君
转载于:https://www.cnblogs.com/fraud/p/4338491.html
poj2778DNA Sequence (AC自动机+矩阵快速幂)相关推荐
- POJ 2778 DNA Sequence [AC自动机 + 矩阵快速幂]
http://poj.org/problem?id=2778 题意:给一些只由ACGT组成的模式串,问有多少种长度为n且不含有给出的模式串的DNA序列. 自动机的状态转换可以看成一个有向图(有重边的) ...
- POJ - 2778 DNA Sequence(AC自动机+矩阵快速幂)
题目链接:点击查看 题目大意:给出 n 个长度不大于 10 的字符串表示病毒串,再给出一个长度 len ,问长度为 len 的字符串中,有多少个字符串不含有病毒串作为子串 题目分析:因为病毒串的长度和 ...
- L. Poor God Water(ACM-ICPC 2018 焦作赛区网络预赛,ac自动机+矩阵快速幂 或 BM线性递推)
描述 God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells hi ...
- HDU 2243考研路茫茫——单词情结 (AC自动机+矩阵快速幂)
背单词,始终是复习英语的重要环节.在荒废了3年大学生涯后,Lele也终于要开始背单词了. 一天,Lele在某本单词书上看到了一个根据词根来背单词的方法.比如"ab",放在单词前一般 ...
- HDU - 2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂)
题目链接:点击查看 题目大意:给出 n 个词根,现在要求出长度不大于 len 的单词中,有多少单词包含至少一个词根 题目分析:如果我们反过来想,也就是求出来总的单词数,然后减去不包含词根的单词数,剩下 ...
- poj 2778 AC自动机+矩阵快速幂
题目链接:https://vjudge.net/problem/POJ-2778 题意:输入n和m表示n个病毒,和一个长为m的字符串,里面只可以有'A','C','G','T' 这四个字符,现在问这个 ...
- POJ 2778 DNA Sequence —— (AC自动机+矩阵快速幂)
距离上次做AC自动机有很久了=.=,以前这题的思路死活看不懂,现在还是觉得很好理解的. 思路参见:http://blog.csdn.net/morgan_xww/article/details/783 ...
- HDU -2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂)
题目链接 思路 假设让求长度为LLL且不包含词根的个数,对所有的词根建acacac自动机,然后用矩阵MMM表示可转移状态,最后最快速幂即可. 如何求长度不超过LLL且不包含LLL且不包含词根的个数,可 ...
- 【BZOJ4861】【BJOI2017】—魔法咒语(AC自动机+矩阵快速幂优化dp)
传送门 当 l ≤ 100 l\le 100 l≤100时 显然的自动机上 d p dp dp就完了 当 l ≤ 1 e 8 l\le1e8 l≤1e8时直接 d p dp dp显然是不行的 但是发现 ...
最新文章
- Windows控制台程序处理消息编程实例二则
- 技术干货 | 如何在 Library 中使用/依赖 mPaaS?
- 2017.5.8 文化之旅 思考记录
- 命名空间“Aspose”中不存在类型或命名空间名称“Slides”。
- 谈谈嵌入式设备用户界面的未来
- 拼多多开放平台订单信息查询接口【pdd.order.basic.list.get订单基础信息列表查询接口(根据成交时间)】代码对接教程
- win10无法装载iso文件_win10系统打开iso格式文件的四种方法
- python sns可视化小技巧(纪录所得)
- 终于搞清楚了:SOLID设计原则出处
- 服务器远程不上怎么办?
- ROC阳性结果还是阴性结果?
- 智能运维探索:有一种多指标异常检测方案,你可能没用过...
- WiFi分销小程序3.0.9独立版
- CF1151F - Sonya and Informatics
- 《es6标准入门》 阮一峰
- 查询选修相同课程的学生学号、课程号和成绩
- 如何用好MindMapper中的便签
- 淮北职业技术学院大一计算机考试,淮北职业技术学院2020年录取分数线(附2018-2020年分数线)...
- 计算机底层:高速缓冲存储器
- Kafka SSL服务配置及客户端使用(Linux+Pykafka)
热门文章
- python截取指定字符串_python 正则匹配获取指定多个词的在字符串(句子/段落)索引位置...
- Java笔记-使用logback按天生成日志并按等级进行分类
- C++设计模式-抽象工厂模式
- OpenCV文档阅读笔记-cvtColor官方解析及实例
- Qt工作笔记-QHash与QMap查找速度粗略比较实战
- MySQL入门之存储过程与存储函数
- C/C++屏幕恶搞程序
- java 8 java demo_Java 8 中的 Streams API Demo
- python慕课笔记_MOOC python笔记(三) 序列容器:字符串、列表、元组
- linux里的run-level,linux run level 为何物