【HDU2222】

最纯粹的裸题,错误点详见注释。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<queue>
  6. using namespace std;
  7. struct ACauto
  8. {
  9. int sum;
  10. ACauto* next[];
  11. ACauto* fail;
  12. ACauto()
  13. {
  14. sum=;
  15. for (int i=;i<;i++) next[i]=NULL;
  16. fail=NULL;
  17. }
  18. };
  19.  
  20. void insert(ACauto* root,char* str)
  21. {
  22. int len=strlen(str);
  23. ACauto* now=root;
  24. for (int i=;i<len;i++)
  25. {
  26. int index=str[i]-'a';
  27. if (now->next[index]==NULL)
  28. {
  29. ACauto* tmp=new ACauto;
  30. now->next[index]=tmp;
  31. }
  32. now=now->next[index];
  33. }
  34. now->sum++;
  35. }
  36.  
  37. void build(ACauto* root)
  38. {
  39. queue<ACauto*> que;
  40. que.push(root);
  41. while (!que.empty())
  42. {
  43. ACauto* tmp=que.front();que.pop();
  44. for (int i=;i<;i++)
  45. {
  46. if (tmp->next[i]==NULL) continue;
  47. if (tmp==root)
  48. tmp->next[i]->fail=root;
  49. else
  50. {
  51. ACauto* p=tmp->fail;
  52. while (p!=NULL)
  53. {
  54. if (p->next[i]!=NULL)
  55. {
  56. tmp->next[i]->fail=p->next[i];
  57. break;
  58. }
  59. p=p->fail;
  60. }
  61. if (p==NULL) tmp->next[i]->fail=root;
  62. }
  63. que.push(tmp->next[i]);
  64. }
  65. }
  66. }
  67.  
  68. int query(ACauto* root,char* str)
  69. {
  70. int ans=,len=strlen(str);
  71. ACauto* p=root;
  72. for (int i=;i<len;i++)
  73. {
  74. int index=str[i]-'a';
  75. while (p->next[index]==NULL && p!=root) p=p->fail;
  76. /*错误点:上述语句是while语句不是if语句*/
  77. p=p->next[index];
  78. p=(p==NULL)?root:p;
  79. ACauto* tmp=p;
  80. while (tmp!=root)
  81. /*-1表示这个单词之前已经被统计过了,不再重复计算*/
  82. {
  83. if (tmp->sum>=)
  84. {
  85. ans+=tmp->sum;
  86. tmp->sum=-;
  87. }
  88. else
  89. break;
  90. tmp=tmp->fail;
  91. }
  92. }
  93. return ans;
  94. }
  95.  
  96. void submain()
  97. {
  98. ACauto* root=new ACauto;
  99. int n;
  100. char str[+];
  101. scanf("%d",&n);
  102. for (int i=;i<n;i++)
  103. {
  104. scanf("%s",str);
  105. insert(root,str);
  106. }
  107. build(root);
  108. scanf("%s",str);
  109. cout<<query(root,str)<<endl;
  110. }
  111.  
  112. int main()
  113. {
  114. int T;
  115. scanf("%d",&T);
  116. for (int kase=;kase<T;kase++) submain();
  117. return ;
  118. }

【AC自动机】HDU中模板题的更多相关文章

  1. hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数

    http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franc ...

  2. NYOJ 1085 数单词 (AC自己主动机模板题)

    数单词 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 为了可以顺利通过英语四六级考试,如今大家每天早上都会早起读英语. LYH本来以为自己在6月份的考试中能够通过六 ...

  3. 【HDU】病毒侵袭(AC自己主动机模板题)

    AC自己主动机的模板题.因为输入的字符串中的字符不保证全为小写字母.所以范围应该在130之前,而前31位字符是不可能出如今字符串的(不懂得查下ACSII表即可了).所以仅仅须要开的结点数组大小为130 ...

  4. 数据结构--AC自动机--hdu 2896

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  5. AC自动机 HDU 2896

    n个字串 m个母串 字串在母串中出现几次 #include<stdio.h> #include<algorithm> #include<string.h> #inc ...

  6. 从0开始 数据结构 AC自动机 hdu 2222

    参考博客 失配指针原理 使当前字符失配时跳转到另一段从root开始每一个字符都与当前已匹配字符段某一个后缀完全相同且长度最大的位置继续匹配,如同KMP算法一样,AC自动机在匹配时如果当前字符串匹配失败 ...

  7. AC自动机 HDU 3065

    大概就是裸的AC自动机了 #include<stdio.h> #include<algorithm> #include<string.h> #include< ...

  8. HDU 2222 Keywords Search(AC自己主动机模板题)

    题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数. 思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以. #include<cstdio> #incl ...

  9. HDU 5384 Danganronpa (AC自己主动机模板题)

    题意:给出n个文本和m个模板.求每一个文本中全部模板出现的总次数. 思路:Trie树权值记录每一个模板的个数.对于每一个文本跑一边find就可以. #include<cstdio> #in ...

随机推荐

  1. C#利用WebClient 两种方式下载文件

    WebClient client = new WebClient(); 第一种 string URLAddress = @"http://files.cnblogs.com/x4646/tr ...

  2. 数据结构--Avl树的创建,插入的递归版本和非递归版本,删除等操作

    AVL树本质上还是一棵二叉搜索树,它的特点是: 1.本身首先是一棵二叉搜索树.   2.带有平衡条件:每个结点的左右子树的高度之差的绝对值最多为1(空树的高度为-1).   也就是说,AVL树,本质上 ...

  3. eComStation 1.2

    https://thomas0008.ctfile.com/u/75519/87485 https://thomas0008.ctfile.com/downhtml/75519/428846/1508 ...

  4. 转 白话解析:一致性哈希算法 consistent hashing

    摘要: 本文首先以一个经典的分布式缓存的应用场景为铺垫,在了解了这个应用场景之后,生动而又不失风趣地介绍了一致性哈希算法,同时也明确给出了一致性哈希算法的优点.存在的问题及其解决办法. 声明与致谢: ...

  5. 2017多校第8场 HDU 6143 Killer Names 容斥,组合计数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意:m种颜色需要为两段长度为n的格子染色,且这两段之间不能出现相同的颜色,问总共有多少种情况. ...

  6. caffe Python API 之 数据输入层(Data,ImageData,HDF5Data)

    import sys sys.path.append('/projects/caffe-ssd/python') import caffe4 net = caffe.NetSpec() 一.Image ...

  7. VPS性能测试(1):CPU物理个数、内核、超线程、多核心

    1.登录VPS界面,执行:cat /proc/cpuinfo,就会显示出VPS主机的CPU详细参数,如内核.频率.型号等等 2.主要参数physical_id表示物理CPU个数,cpu cores是内 ...

  8. 我的新博客地址http://xxxbw.github.io/

    最近在学github,在github搭了个博客,以后也会使用另外一个博客.有兴趣的小伙伴可以看看~ 地址:http://xxxbw.github.io/

  9. 2017百度春招<度度熊买帽子的问题>

    题目: 度度熊想去商场买一顶帽子,商场里有N顶帽子,有些帽子的价格可能相同.度度熊想买一顶价格第三便宜的帽子,问第三便宜的帽子价格是多少? 数组中找到第三小的数字  注意边界条件 用STL中的set来 ...

  10. 【JBPM4】任务节点-任务分配swimlane

    swimlane泳道,几个任务受理人相同的任务节点,可以划分为一个泳道 JPDL <?xml version="1.0" encoding="UTF-8" ...