转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

DNA Sequence
Time Limit: 1000MS   Memory Limit: 65536K

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

POJ Monthly--2006.03.26,dodo
题意
构造一个长度为n的DNA序列,要求其中不得出现m个禁止的字符串中的任意一个
一道很明显的矩阵快速幂的题。先通过AC自动机得出一个邻接矩阵,然后快速幂。

  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自动机+矩阵快速幂)相关推荐

  1. POJ 2778 DNA Sequence [AC自动机 + 矩阵快速幂]

    http://poj.org/problem?id=2778 题意:给一些只由ACGT组成的模式串,问有多少种长度为n且不含有给出的模式串的DNA序列. 自动机的状态转换可以看成一个有向图(有重边的) ...

  2. POJ - 2778 DNA Sequence(AC自动机+矩阵快速幂)

    题目链接:点击查看 题目大意:给出 n 个长度不大于 10 的字符串表示病毒串,再给出一个长度 len ,问长度为 len 的字符串中,有多少个字符串不含有病毒串作为子串 题目分析:因为病毒串的长度和 ...

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

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

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

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

    题目链接:点击查看 题目大意:给出 n 个词根,现在要求出长度不大于 len 的单词中,有多少单词包含至少一个词根 题目分析:如果我们反过来想,也就是求出来总的单词数,然后减去不包含词根的单词数,剩下 ...

  6. poj 2778 AC自动机+矩阵快速幂

    题目链接:https://vjudge.net/problem/POJ-2778 题意:输入n和m表示n个病毒,和一个长为m的字符串,里面只可以有'A','C','G','T' 这四个字符,现在问这个 ...

  7. POJ 2778 DNA Sequence —— (AC自动机+矩阵快速幂)

    距离上次做AC自动机有很久了=.=,以前这题的思路死活看不懂,现在还是觉得很好理解的. 思路参见:http://blog.csdn.net/morgan_xww/article/details/783 ...

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

    题目链接 思路 假设让求长度为LLL且不包含词根的个数,对所有的词根建acacac自动机,然后用矩阵MMM表示可转移状态,最后最快速幂即可. 如何求长度不超过LLL且不包含LLL且不包含词根的个数,可 ...

  9. 【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显然是不行的 但是发现 ...

最新文章

  1. Windows控制台程序处理消息编程实例二则
  2. 技术干货 | 如何在 Library 中使用/依赖 mPaaS?
  3. 2017.5.8 文化之旅 思考记录
  4. 命名空间“Aspose”中不存在类型或命名空间名称“Slides”。
  5. 谈谈嵌入式设备用户界面的未来
  6. 拼多多开放平台订单信息查询接口【pdd.order.basic.list.get订单基础信息列表查询接口(根据成交时间)】代码对接教程
  7. win10无法装载iso文件_win10系统打开iso格式文件的四种方法
  8. python sns可视化小技巧(纪录所得)
  9. 终于搞清楚了:SOLID设计原则出处
  10. 服务器远程不上怎么办?
  11. ROC阳性结果还是阴性结果?
  12. 智能运维探索:有一种多指标异常检测方案,你可能没用过...
  13. WiFi分销小程序3.0.9独立版
  14. CF1151F - Sonya and Informatics
  15. 《es6标准入门》 阮一峰
  16. 查询选修相同课程的学生学号、课程号和成绩
  17. 如何用好MindMapper中的便签
  18. 淮北职业技术学院大一计算机考试,淮北职业技术学院2020年录取分数线(附2018-2020年分数线)...
  19. 计算机底层:高速缓冲存储器
  20. Kafka SSL服务配置及客户端使用(Linux+Pykafka)

热门文章

  1. python截取指定字符串_python 正则匹配获取指定多个词的在字符串(句子/段落)索引位置...
  2. Java笔记-使用logback按天生成日志并按等级进行分类
  3. C++设计模式-抽象工厂模式
  4. OpenCV文档阅读笔记-cvtColor官方解析及实例
  5. Qt工作笔记-QHash与QMap查找速度粗略比较实战
  6. MySQL入门之存储过程与存储函数
  7. C/C++屏幕恶搞程序
  8. java 8 java demo_Java 8 中的 Streams API Demo
  9. python慕课笔记_MOOC python笔记(三) 序列容器:字符串、列表、元组
  10. linux里的run-level,linux run level 为何物