首先要明白AC自动机是干什么的:

AC自动机其实就是一种多模匹配算法,那么你可能会问什么叫做多模匹配算法。下面是我对多模匹配的理解,与多模与之对于的是单模,单模就是给你一个单词,然后给你一个字符串,问你这个单词是否在这个字符串中出现过(匹配),这个问题可以用kmp算法在比较高效的效率上完成这个任务。那么现在我们换个问题,给你很多个单词,然后给你一段字符串,问你有多少个单词在这个字符串中出现过,当然我们暴力做,用每一个单词对字符串做kmp,这样虽然理论上可行,但是时间复杂度非常之高,当单词的个数比较多并且字符串很长的情况下不能有效的解决这个问题,所以这时候就要用到我们的ac自动机算法了。

一个母字符串,多个子字符串与其多次匹配,就是它的用处。

这里有很多例题:AC自动机小结


例题1:HDU2222

  1. //======================
  2. // HDU 2222
  3. // 求目标串中出现了几个模式串
  4. //====================
  5. #include <stdio.h>
  6. #include <algorithm>
  7. #include <iostream>
  8. #include <string.h>
  9. #include <queue>
  10. using namespace std;
  11. struct Trie
  12. {
  13. int next[500010][26],fail[500010],end[500010];
  14. int root,L;
  15. int newnode()
  16. {
  17. for(int i = 0;i < 26;i++)
  18. next[L][i] = -1;
  19. end[L++] = 0;
  20. return L-1;
  21. }
  22. void init()
  23. {
  24. L = 0;
  25. root = newnode();
  26. }
  27. void insert(char buf[])
  28. {
  29. int len = strlen(buf);
  30. int now = root;
  31. for(int i = 0;i < len;i++)
  32. {
  33. if(next[now][buf[i]-'a'] == -1)
  34. next[now][buf[i]-'a'] = newnode();
  35. now = next[now][buf[i]-'a'];
  36. }
  37. end[now]++;
  38. }
  39. void build()
  40. {
  41. queue<int>Q;
  42. fail[root] = root;
  43. for(int i = 0;i < 26;i++)
  44. if(next[root][i] == -1)
  45. next[root][i] = root;
  46. else
  47. {
  48. fail[next[root][i]] = root;
  49. Q.push(next[root][i]);
  50. }
  51. while( !Q.empty() )
  52. {
  53. int now = Q.front();
  54. Q.pop();
  55. for(int i = 0;i < 26;i++)
  56. if(next[now][i] == -1)
  57. next[now][i] = next[fail[now]][i];
  58. else
  59. {
  60. fail[next[now][i]]=next[fail[now]][i];
  61. Q.push(next[now][i]);
  62. }
  63. }
  64. }
  65. int query(char buf[])
  66. {
  67. int len = strlen(buf);
  68. int now = root;
  69. int res = 0;
  70. for(int i = 0;i < len;i++)
  71. {
  72. now = next[now][buf[i]-'a'];
  73. int temp = now;
  74. while( temp != root )
  75. {
  76. res += end[temp];
  77. end[temp] = 0;//如果这里没有清0(删去),那么就会变成重复统计
  78. temp = fail[temp];
  79. }
  80. }
  81. return res;
  82. }
  83. void debug()
  84. {
  85. for(int i = 0;i < L;i++)
  86. {
  87. printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
  88. for(int j = 0;j < 26;j++)
  89. printf("%2d",next[i][j]);
  90. printf("]\n");
  91. }
  92. }
  93. };
  94. char buf[1000010];
  95. Trie ac;
  96. int main()
  97. {
  98. int T;
  99. int n;
  100. scanf("%d",&T);
  101. while( T-- )
  102. {
  103. scanf("%d",&n);
  104. ac.init();
  105. for(int i = 0;i < n;i++)
  106. {
  107. scanf("%s",buf);
  108. ac.insert(buf);
  109. }
  110. ac.build();
  111. scanf("%s",buf);
  112. printf("%d\n",ac.query(buf));
  113. }
  114. return 0;
  115. }

例题2:HDU5384

给你一堆母串,还有另一堆子串,询问每个母串中出现的子串总次数。(子串计数可重叠)

注:下面这个代码是阉割版的,需要将a变为a[maxn]数组同时遍历每个母串才可以使用。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<string.h>
  4. #include<algorithm>
  5. #include<queue>
  6. using namespace std;
  7. const int maxn = 1e6 + 5;
  8. int n;
  9. char a[maxn];
  10. char s[maxn];
  11. struct trie {
  12. int next[maxn][26], fail[maxn], end[maxn];
  13. int root, cnt;
  14. int new_node() {
  15. memset(next[cnt], -1, sizeof next[cnt]);
  16. end[cnt++] = 0;
  17. return cnt - 1;
  18. }
  19. void init() {
  20. cnt = 0;
  21. root = new_node();
  22. }
  23. void insert(char *buf) {//字典树插入一个单词
  24. int len = strlen(buf);
  25. int now = root;
  26. for (int i = 0; i < len; i++) {
  27. int id = buf[i] - 'a';
  28. if (next[now][id] == -1) {
  29. next[now][id] = new_node();
  30. }
  31. now = next[now][id];
  32. }
  33. end[now]++;
  34. }
  35. void build() {//构建fail指针
  36. queue <int> q;
  37. fail[root] = root;
  38. for (int i = 0; i < 26; i++) {
  39. if (next[root][i] == -1) {
  40. next[root][i] = root;
  41. }
  42. else {
  43. fail[next[root][i]] = root;
  44. q.push(next[root][i]);
  45. }
  46. }
  47. while (!q.empty()) {
  48. int now = q.front(); q.pop();
  49. for (int i = 0; i < 26; i++) {
  50. if (next[now][i] == -1) {
  51. next[now][i] = next[fail[now]][i];
  52. }
  53. else {
  54. fail[next[now][i]] = next[fail[now]][i];
  55. q.push(next[now][i]);
  56. }
  57. }
  58. }
  59. }
  60. int query(string buf) {
  61. int len = buf.length();
  62. int now = root;
  63. int res = 0;
  64. for (int i = 0; i < len; i++) {
  65. int id = buf[i] - 'a';
  66. now = next[now][id];
  67. int tmp = now;
  68. while (tmp != root) {
  69. res += end[tmp];
  70. tmp = fail[tmp];
  71. }
  72. }
  73. return res;
  74. }
  75. }ac;
  76. int main() {
  77. int T;
  78. scanf("%d", &T);
  79. while (T--) {
  80. ac.init();
  81. scanf("%s", a);
  82. scanf("%d", &n);
  83. for (int i = 0; i < n; i++) {
  84. scanf("%s", s);
  85. ac.insert(s);
  86. }
  87. ac.build();
  88. printf("%d\n", ac.query(a));
  89. }
  90. return 0;
  91. }

例题3:POJ2778

神仙打架、神仙打架。。。。

题意:

•题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列。(仅含A,T,C,G四个字符)

•样例m=4,n=3,{“AA”,”AT”,”AC”,”AG”}

•答案为36,表示有36种长度为3的序列可以不包含疾病

简单来说就是:

算了我不说了。。。。看上面的讲解和代码吧

  1. //https://blog.csdn.net/u013446688/article/details/47378255
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <queue>
  5. #include <cstring>
  6. using namespace std;
  7. const int MOD = 100000;
  8. struct Matrix{
  9. int mat[110][110], n;
  10. Matrix(){}
  11. Matrix(int _n){
  12. n = _n;
  13. for(int i = 0; i < n; i++)
  14. for(int j = 0; j < n; j++)
  15. mat[i][j] = 0;
  16. }
  17. Matrix operator *(const Matrix &b) const{
  18. Matrix ret = Matrix(n);
  19. for(int i = 0; i < n; i++)
  20. for(int j = 0; j < n; j++)
  21. for(int k = 0; k < n; k++){
  22. int tmp = (long long)mat[i][k] * b.mat[k][j] % MOD;
  23. ret.mat[i][j] = (ret.mat[i][j] + tmp) % MOD;
  24. }
  25. return ret;
  26. }
  27. };
  28. struct Trie{
  29. int next[110][4], fail[110];
  30. bool end[110];
  31. int root, L;
  32. int newnode(){
  33. for(int i = 0; i < 4; i++) next[L][i] = -1;
  34. end[L++] = false;
  35. return L-1;
  36. }
  37. void init(){
  38. L = 0;
  39. root = newnode();
  40. }
  41. int getch(char ch){
  42. if(ch == 'A') return 0;
  43. if(ch == 'C') return 1;
  44. if(ch == 'G') return 2;
  45. else return 3;
  46. }
  47. void insert(char s[]){
  48. int len = strlen(s);
  49. int now = root;
  50. for(int i = 0; i < len; i++){
  51. if(next[now][getch(s[i])] == -1)
  52. next[now][getch(s[i])] = newnode();
  53. now = next[now][getch(s[i])];
  54. }
  55. end[now] = true;
  56. }
  57. void build(){
  58. queue<int> Q;
  59. for(int i = 0; i < 4; i ++){
  60. if(next[root][i] == -1) next[root][i] = root;
  61. else{
  62. fail[ next[root][i] ] = root;
  63. Q.push(next[root][i]);
  64. }
  65. }
  66. while(!Q.empty()){
  67. int now = Q.front();
  68. Q.pop();
  69. if(end[ fail[now] ] == true) end[now] = true;
  70. for(int i = 0; i < 4; i ++){
  71. if(next[now][i] == -1)
  72. next[now][i] = next[ fail[now] ][i];
  73. else{
  74. fail[ next[now][i] ] = next[ fail[now] ][i];
  75. Q.push(next[now][i]);
  76. }
  77. }
  78. }
  79. }
  80. Matrix getMatrix(){
  81. Matrix ret = Matrix(L);
  82. for(int i = 0; i < L; i ++)
  83. for(int j = 0; j < 4; j ++)
  84. if(end[ next[i][j] ] == false)
  85. ret.mat[i][ next[i][j] ] ++;
  86. return ret;
  87. }
  88. };
  89. Trie ac;
  90. char buf[20];
  91. Matrix pow_Mat(Matrix a, int n){ //快速幂
  92. Matrix ret = Matrix(a.n);
  93. for(int i = 0; i < ret.n; i ++) ret.mat[i][i] = 1;
  94. Matrix tmp = a;
  95. while(n){
  96. if(n & 1) ret = ret * tmp;
  97. tmp = tmp * tmp;
  98. n >>= 1;
  99. }
  100. return ret;
  101. }
  102. int main(){
  103. #ifdef sxk
  104. freopen("in.txt", "r", stdin);
  105. #endif //sxk
  106. int n, m;
  107. while(scanf("%d%d", &m, &n) == 2){
  108. ac.init();
  109. for(int i = 0; i < m; i ++){
  110. scanf("%s", buf);
  111. ac.insert(buf);
  112. }
  113. ac.build();
  114. Matrix a = ac.getMatrix();
  115. a = pow_Mat(a, n);
  116. int ans = 0;
  117. for(int i = 0; i < a.n; i ++)
  118. ans = (ans + a.mat[0][i]) % MOD;
  119. printf("%d\n", ans);
  120. }
  121. return 0;
  122. }

AC自动机(模板+例题)的更多相关文章

  1. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  2. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  3. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  4. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  5. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  6. HDU 2222 (AC自动机模板题)

    题意: 给一个文本串和多个模式串,求文本串中一共出现多少次模式串 分析: ac自动机模板,关键是失配函数 #include <map> #include <set> #incl ...

  7. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  8. KMP与AC自动机模板

    HDU 1711 Number Sequence(KMP模板题) http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<bits/std ...

  9. HDU3695(AC自动机模板题)

    题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...

  10. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

随机推荐

  1. Kali桥接模式下配置ip

    以管理员身份运行虚拟机 打开控制面板-->网络和Internet-->更改适配器 再在虚拟机处桥接到这个WLAN2 点击 编辑-->编辑虚拟网卡 没有网卡就点上图的添加网络作为桥接网 ...

  2. 一步步搭建jumpserver

    测试推荐环境 CPU: 64位双核处理器 内存: 4G DDR3 数据库:mysql 版本大于等于 5.6 mariadb 版本大于等于 5.5.6 环境 系统: CentOS 7 IP: 192.1 ...

  3. 【5min+】 设计模式的迷惑?Provider vs Factory

    系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...

  4. 吐槽一下python

    关于python,优点有很多.例如,编码灵活,书写随意. 印象最深的就是,Duck Type.也就说,如果使用会走路和会飞来衡量鸭子, 那么如果一个物体,走路像鸭子,飞起来像鸭子,那么它就是鸭子. d ...

  5. Node.js的__dirname,__filename,process.cwd(),./的一些坑

    参考博客:https://github.com/jawil/blog/issues/18

  6. bootstrap-daterangepicker

    1,依赖关系 使用之前需要引用bootstrap.css   daterangpicker.css    jquery.js   boostrap.js  moment.js   daterangpi ...

  7. js this是什么?[多次书写]

    前言 以前的时候,我写了一个关于js this的博客,写的非常复杂,分析了各种情况. 现在我想简化. 如果你有后台基础,专门去理解过this,那么请忘记. 这东西是有口诀的: 在方法中,this 表示 ...

  8. cookie的设置与取值

    设置cookie function cookie(key, value, options) { let days let time let result // A key and value were ...

  9. springboot无法访问静态资源

    无法访问static下的静态资源 1.在application.yml中添加 resources: static-locations: classpath:/META-INF/resources/,c ...

  10. PVE裸机虚拟化环境安装之后的一些部署记录

    pve镜像使用的是proxmox-ve_6.1-1 安装之后root登录 apt update 更新源的时候会出现一些问题,是因为其中有一个企业源报错的原因 安装sudo和vim,否则不好管理非roo ...