POJ 2778 DNA Sequence

Problem : 给m个只含有(A,G,C,T)的模式串(m <= 10, len <=10), 询问所有长度为n的只含有(A,G,C,T)的串中有多少个不含有模式串的串。(n<=2000000000)

Solution :首先对所有模式串建立AC自动机。然后dp[i][j]表示长度为i,走到AC自动机的节点j这样的字符串满足条件的个数有多少,用AC自动机的边写出状态转移方程然后用矩阵快速幂加速运算。

  1. #include <iostream>
  2. #include <string>
  3. #include <queue>
  4. using namespace std;
  5. const int N = 208;
  6. const int mo = 100000;
  7. int id[128];
  8. struct Matrix
  9. {
  10. int n;
  11. int a[N][N];
  12. Matrix(int n_, int p)
  13. {
  14. n = n_;
  15. for (int i = 0; i < n; ++i)
  16. for (int j = 0; j < n; ++j)
  17. {
  18. a[i][j] = 0;
  19. if (i == j) a[i][j] = p;
  20. }
  21. }
  22. friend Matrix operator *(Matrix A, Matrix B)
  23. {
  24. Matrix C(A.n, 0);
  25. for (int i = 0; i < A.n; ++i)
  26. for (int j = 0; j < A.n; ++j)
  27. for (int k = 0; k < A.n; k++)
  28. C.a[i][j] = (C.a[i][j] + 1ll * A.a[i][k] * B.a[k][j] % mo) % mo;
  29. return C;
  30. }
  31. void print()
  32. {
  33. for (int i = 0; i < n; ++i)
  34. {
  35. for (int j = 0; j < n; ++j) cout << a[i][j] << " ";
  36. cout << endl;
  37. }
  38. }
  39. };
  40. struct AC_Automan
  41. {
  42. int next[N][4];
  43. int fail[N];
  44. int cnt[N];
  45. int root, tot;
  46. int newnode()
  47. {
  48. for (int i = 0; i <= 3; ++i) next[tot][i] = -1;
  49. fail[tot] = cnt[tot] = -1;
  50. return tot++;
  51. }
  52. void clear()
  53. {
  54. tot = 0;
  55. root = newnode();
  56. }
  57. void insert(const string &s)
  58. {
  59. int p = root;
  60. for (int i = 0, len = s.length(); i < len; ++i)
  61. {
  62. if (next[p][id[s[i]]] == -1) next[p][id[s[i]]] = newnode();
  63. p = next[p][id[s[i]]];
  64. }
  65. cnt[p] = 1;
  66. }
  67. void build()
  68. {
  69. queue <int> Q;
  70. Q.push(root);
  71. while (!Q.empty())
  72. {
  73. int p = Q.front(); Q.pop();
  74. for (int i = 0; i < 4; ++i)
  75. {
  76. if (~next[p][i])
  77. {
  78. if (p == root) fail[next[p][i]] = root;
  79. else fail[next[p][i]] = next[fail[p]][i];
  80. Q.push(next[p][i]);
  81. }
  82. else
  83. {
  84. if (p == root) next[p][i] = root;
  85. else next[p][i] = next[fail[p]][i];
  86. }
  87. }
  88. }
  89. }
  90. Matrix power(Matrix A, int y)
  91. {
  92. Matrix B(tot, 1);
  93. while (y)
  94. {
  95. if (y & 1) B = B * A;
  96. A = A * A;
  97. y >>= 1;
  98. }
  99. return B;
  100. }
  101. void solve(int num)
  102. {
  103. Matrix A(tot, 0);
  104. for (int i = 0; i <= tot; ++i)
  105. {
  106. for (int j = 0; j < 4; ++j)
  107. {
  108. int flag = 1;
  109. for (int temp = next[i][j]; temp != root; temp = fail[temp])
  110. {
  111. if (~cnt[temp]) flag = 0;
  112. }
  113. A.a[i][next[i][j]] += flag;
  114. }
  115. }
  116. A = power(A, num);
  117. int ans = 0;
  118. for (int i = 0; i < tot; ++i) ans = (ans + A.a[0][i]) % mo;
  119. cout << ans << endl;
  120. }
  121. }ac;
  122. int main()
  123. {
  124. cin.sync_with_stdio(0);
  125. id['A'] = 0; id['G'] = 1; id['C'] = 2; id['T'] = 3;
  126. int m, n;
  127. while (cin >> m >> n)
  128. {
  129. ac.clear();
  130. for (int i = 1; i <= m; ++i)
  131. {
  132. string s; cin >> s;
  133. ac.insert(s);
  134. }
  135. ac.build();
  136. ac.solve(n);
  137. }
  138. }

POJ 2778 (AC自动机+矩阵乘法)的更多相关文章

  1. DNA Sequence POJ - 2778 AC 自动机 矩阵乘法

    定义重载运算的时候一定要将矩阵初始化,因为这个调了一上午...... Code: #include<cstdio> #include<algorithm> #include&l ...

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

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

  3. DNA Sequence POJ - 2778 AC自动机 && 矩阵快速幂

    It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's very useful to ...

  4. 【bzoj1444】[Jsoi2009]有趣的游戏 AC自动机+矩阵乘法

    题目描述 输入 注意 是0<=P 输出 样例输入 样例输出 题解 AC自动机+矩阵乘法 先将所有字符串放到AC自动机中,求出Trie图. 然后构建邻接矩阵:如果x不是某个字符串的末位置,则x连向 ...

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

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

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

    题目大意:DNA序列是有 ATGC 组成的,现在知道一些动物的遗传片段有害的,那么如果给出这些有害的片段,能否求出来所有长度为 N 的基因中有多少是不包含这些有害片段的.   分析:也是断断续续做了一 ...

  7. [BZOJ 1009] [HNOI2008] GT考试 【AC自动机 + 矩阵乘法优化DP】

    题目链接:BZOJ - 1009 题目分析 题目要求求出不包含给定字符串的长度为 n 的字符串的数量. 既然这样,应该就是 KMP + DP ,用 f[i][j] 表示长度为 i ,匹配到模式串第 j ...

  8. 【POJ2778】AC自动机+矩阵乘法

    DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14758 Accepted: 5716 Descrip ...

  9. bzoj 2553: [BeiJing2011]禁忌 AC自动机+矩阵乘法

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=2553 题解: 利用AC自动机的dp求出所有的转移 然后将所有的转移储存到矩阵中,进行矩阵 ...

随机推荐

  1. .NET通过字典给类赋值

    /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam& ...

  2. 《基于Node.js实现简易聊天室系列之项目前期工作》

    前期工作主要包括:项目的创建,web服务器的创建和数据库的连接. 项目创建 网上关于Node.js项目的创建的教程有很多,这里不必赘述.Demo所使用的Node.js的框架是express,版本为4. ...

  3. 在WIndowsPhone8 上制作的简单的计算器

    今天,闲着没事,就自己做了一个小小的计算器...虽说自己刚学wp8开发没多长时间,望大神多多指教..1.这是前台页面的代码 <Grid x:Name=" Margin="10 ...

  4. 第二章 TCP/IP 基础知识

    第二章 TCP/IP 基础知识   TCP/IP  transmission control protocol and ip internet protocol 是互联网众多通信协议中最为著名的.   ...

  5. iOS---iPad开发及iPad特有的特技

    iPad开发简单介绍 iPad开发最大的不同在于iPhone的就是屏幕控件的适配,以及横竖屏的旋转. Storyboard中得SizeClass的横竖屏配置,也不支持iPad开发. 1.在控制器中得到 ...

  6. js模块化方案以及前端打包工具

    图片来自知乎

  7. 读取Java文件到byte数组的三种方式

    package zs; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io ...

  8. 为什么jfinal的控制器不用单例模式

    先假controller定采用单例模式,通常两种设计方式来存放 HttpServletRequest.HttpServletResponse 等对象,一是利用一个类似于 ActionContext 的 ...

  9. Linux 时间同步 ntpdate

    ntpdate 使用网络计时协议(NTP)设置日期和时间.此命令仅应用于 AIX 4.2 或后期版本. 语法: ntpdate [ -b] [ -d] [ -s] [ -u] [ -aKeyid] [ ...

  10. C++学习随笔

    今天试着变了下实验二里边的有关面向对象的实验,深深地觉得我对面向对象的编程的理解还是很浅显,以至于对于对象的调用也是瞎整.居然直接就去调用继承来的函数,连生成一个对象这种基础应用都不知道.对自己的基础 ...