Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12726   Accepted: 4862

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

  1. 4 3
  2. AT
  3. AC
  4. AG
  5. AA

Sample Output

  1. 36

Source

推荐一个网址 :Maxtrix67

  1. /**
  2. 题意:给出n个字符串,问长度为m的字符串中有多少没有包含病毒
  3. 做法:AC自动机 + 矩阵快速幂
  4. Maxtrix 67 给出的定理8
  5. 经典题目8 给定一个有向图,问从A点恰好走k步(允许重复经过边)到达B点的方案数mod p的值
  6. 把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j。 令C=A*A,
  7. 那么C(i,j)=ΣA(i,k)*A(k,j),实际上就等于从点i到点j恰好经过2条边的路径数(枚举k为中转点)。
  8. 类似地,C*A的第i行第j列就表示从i到j经过3条边的路径数。同理,如果要求经过k步的路径数,
  9. 我们只需要二分求出A^k即可。
  10. **/
  11. #include <iostream>
  12. #include <cmath>
  13. #include <stdio.h>
  14. #include <algorithm>
  15. #include <string.h>
  16. #include <queue>
  17. #include <map>
  18. #define MM 10
  19. #define mod 100000
  20. using namespace std;
  21. struct Matrix
  22. {
  23. unsigned long long mat[][];
  24. int n;
  25. Matrix(){}
  26. Matrix(int _n)
  27. {
  28. n = _n;
  29. for(int i=;i<n;i++)
  30. {
  31. for(int j=;j<n;j++)
  32. {
  33. mat[i][j] = ;
  34. }
  35. }
  36. }
  37. Matrix operator *(const Matrix &b) const
  38. {
  39. Matrix res = Matrix(n);
  40. for(int i=;i<n;i++)
  41. {
  42. for(int j=;j<n;j++)
  43. {
  44. res.mat[i][j] = ;
  45. for(int k=;k<n;k++)
  46. {
  47. res.mat[i][j] += mat[i][k] * b.mat[k][j];
  48. }
  49. res.mat[i][j] %= mod;
  50. }
  51. }
  52. return res;
  53. }
  54. };
  55. unsigned long long quick_pow(unsigned long long a,int n)
  56. {
  57. unsigned long long res = ;
  58. unsigned long long tmp = a;
  59. while(n)
  60. {
  61. if(n&) res *= tmp;
  62. tmp *= tmp;
  63. n >>= ;
  64. }
  65. return res;
  66. }
  67. Matrix quick_pow(Matrix a,int n)
  68. {
  69. Matrix res = Matrix(a.n);
  70. for(int i=;i<a.n;i++)
  71. {
  72. res.mat[i][i] = ;
  73. }
  74. Matrix tmp = a;
  75. while(n)
  76. {
  77. if(n&) res =res * tmp;
  78. tmp = tmp * tmp;
  79. n >>= ;
  80. }
  81. return res;
  82. }
  83. struct Tire
  84. {
  85. int next[][MM],fail[];
  86. bool end[];
  87. int L,root;
  88. map<char,int>id;
  89. int newnode()
  90. {
  91. for(int i=;i<MM;i++)
  92. {
  93. next[L][i] = -;
  94. }
  95. end[L++] = ;
  96. return L-;
  97. }
  98. void init()
  99. {
  100. L = ;
  101. root = newnode();
  102. id['A'] = ;
  103. id['T'] = ;
  104. id['C'] = ;
  105. id['G'] = ;
  106. }
  107. void insert(char buf[])
  108. {
  109. int now = root;
  110.  
  111. int len = strlen(buf);
  112. for(int i=;i<len;i++)
  113. {
  114. if(next[now][id[buf[i]]] == -)
  115. next[now][id[buf[i]]] = newnode();
  116. now = next[now][id[buf[i]]];
  117. }
  118. end[now] = true;
  119. }
  120. void build()
  121. {
  122. queue<int>que;
  123. int now = root;
  124. fail[root] = root;
  125. for(int i=;i<;i++)
  126. {
  127. if(next[now][i] == -)
  128. next[now][i] = root;
  129. else
  130. {
  131. fail[next[now][i]] = root;
  132. que.push(next[now][i]);
  133. }
  134. }
  135. while(!que.empty())
  136. {
  137. now = que.front();
  138. que.pop();
  139. if(end[fail[now]]) end[now] = true;
  140. for(int i=;i<;i++)
  141. {
  142. if(next[now][i] == -)
  143. next[now][i] = next[fail[now]][i];
  144. else
  145. {
  146. fail[next[now][i]] = next[fail[now]][i];
  147. que.push(next[now][i]);
  148. }
  149. }
  150. }
  151. }
  152. Matrix getMatrix()
  153. {
  154. Matrix res = Matrix(L+);
  155. for(int i=;i<L;i++)
  156. {
  157. for(int j=;j<;j++)
  158. {
  159. if(end[next[i][j]] == false && !end[i])
  160. res.mat[i][next[i][j]] ++;
  161. }
  162. }
  163. return res;
  164. }
  165. };
  166. char buf[];
  167. Tire ac;
  168. int main()
  169. {
  170. // freopen("in.txt","r",stdin);
  171. int n,m;
  172. while(~scanf("%d %d",&n,&m))
  173. {
  174. ac.init();
  175. for(int i=;i<n;i++)
  176. {
  177. scanf("%s",buf);
  178. ac.insert(buf);
  179. }
  180. ac.build();
  181. Matrix a = ac.getMatrix();
  182. a = quick_pow(a,m);
  183. unsigned long long res = ;
  184. for(int i=;i<a.n;i++)
  185. {
  186. res += a.mat[][i];
  187. }
  188. cout<<res%mod<<endl;
  189. }
  190. return ;
  191. }

POJ-2778的更多相关文章

  1. poj 2778 AC自己主动机 + 矩阵高速幂

    // poj 2778 AC自己主动机 + 矩阵高速幂 // // 题目链接: // // http://poj.org/problem?id=2778 // // 解题思路: // // 建立AC自 ...

  2. POJ 2778 (AC自动机+矩阵乘法)

    POJ 2778 DNA Sequence Problem : 给m个只含有(A,G,C,T)的模式串(m <= 10, len <=10), 询问所有长度为n的只含有(A,G,C,T)的 ...

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

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

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

    题目链接:http://poj.org/problem?id=2778 题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) ...

  5. POJ 2778 AC自己主动机+矩阵幂 不错的题

    http://poj.org/problem?id=2778 有空再又一次做下,对状态图的理解非常重要 题解: http://blog.csdn.net/morgan_xww/article/deta ...

  6. POJ 2778 DNA Sequence(AC自动机+矩阵)

    [题目链接] http://poj.org/problem?id=2778 [题目大意] 给出一些字符串,求不包含这些字符串的长度为n的字符串的数量 [题解] 我们将所有串插入自动机计算match,对 ...

  7. POJ 2778:DNA Sequence(AC自动机构造矩阵)

    http://poj.org/problem?id=2778 题意:有m个病毒DNA,问构造一个长度为n的不带病毒DNA的字符串可以有多少种. 思路:看到这题有点懵,想了挺久题解的思路. 使用AC自动 ...

  8. POJ 2778 DNA Sequence(AC自动机+矩阵加速)

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9899   Accepted: 3717 Desc ...

  9. POJ 2778 DNA sequence

    QAQ 做完禁忌 又做过文本生成器 这道题就是个水题啦 首先转移方程还是文本生成器的转移方程 但是注意到L很大,但是节点数很小 转移都是固定的,所以我们可以用AC自动机来构造转移矩阵 之后进行矩阵乘法 ...

  10. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

随机推荐

  1. 关于wesocket大文件通讯的切片实现方法

    关于websocket的实现网上很多资料这里就不详说,这里大概讲我在websocket传输大文件的时的方法,websocket传输单个文件最大不能超过7kg,否则前段自动断掉,当我们用来语音通讯时,通 ...

  2. 15ecjtu校赛1006 (dfs容斥)

    Problem Description 在平面上有一个n*n的网格,即有n条平行于x轴的直线和n条平行于y轴的直线,形 成了n*n个交点(a,b)(1<=a<=n,1<=b<= ...

  3. D-query SPOJ - DQUERY(莫队)统计不同数的数量

    Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) ...

  4. eclipse的最新版本luna的中建立svn和maven

    http://blog.csdn.net/notillusion/article/details/40950185

  5. expect使用小结

    因为工作关系,需要经常从线上机器上拉取数据,于是想着能否写个脚本,自动完成这个任务呢? 我一般使用scp在机器间传输文件,然而每次scp都需要输入密码,自动化脚本怎么解决这个问题呢?于是expect这 ...

  6. CodeVS 1017 DP

    1017 乘积最大 2000年NOIP全国联赛普及组NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描 ...

  7. SNS应用好友动态Feed模块设计

    转载自:http://libo93122.blog.163.com/blog/static/122189382012112145728902/ 备注:找不到原作者了. 现在大部分SNS网站都有一个功能 ...

  8. MySQL新建用户,授权

    登录MySQL mysql -u root -p 添加新用户 允许本地 IP 访问 localhost, 127.0.0.1 create user 'test'@'localhost' identi ...

  9. LightOJ 1085 - All Possible Increasing Subsequences 树状数组+离散

    http://www.lightoj.com/volume_showproblem.php?problem=1085 题意:求一个序列的递增子序列个数. 思路:找规律可以发现,某个数作为末尾数的种类数 ...

  10. Php扩展--seasLog日志扩展安装

    安装/配置 编译安装 wge thttp://pecl.php.net/get/SeasLog-1.4.4.tgz tar -zxvfSeasLog-1.4.4.tgz cd SeasLog-1.4. ...