题目链接:

pid=2222">点击进入

KMP对模式串进行处理。然后就能够方便的推断模式串是否在目标串中出现了;这显示适合一个模式串多个目标串的情况。可是假设模式串有多个,这时假设还用KMP则须要对每一个串都进行一次处理,显然不是非常合适。事实上这时候能够将全部模式串建成一棵trie树。然后採用相似于kmp的方法计算出failed指针,也就能够方便的进行匹配了。事实上这就是ac自己主动机的思想。

代码例如以下:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<queue>
  5. using namespace std;
  6. const int maxn=500000+100;
  7. struct Trie
  8. {
  9. ///next数组表示的就是trie树
  10. ///fail数组记录了每一个节点的失败指针
  11. ///end数组记录了以当前节点作为结尾的字符串的个数
  12. int next[maxn][26],fail[maxn],end[maxn];
  13. ///root表示trie的根节点。L表示当前trie的节点总数
  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. ///採用bfs形式构造fail指针
  40. void build()
  41. {
  42. queue<int>Q;
  43. fail[root]=root;
  44. for(int i=0;i<26;i++)
  45. {
  46. ///假设当前节点的第i个儿子不存在
  47. ///则将其补上,为当前节点失败节点的第i个儿子
  48. if(next[root][i]==-1)
  49. next[root][i]=root;
  50. else
  51. {
  52. fail[next[root][i]]=root;
  53. Q.push(next[root][i]);
  54. }
  55. }
  56. while(!Q.empty())
  57. {
  58. int now=Q.front();
  59. Q.pop();
  60. for(int i=0;i<26;i++)
  61. {
  62. if(next[now][i]==-1)
  63. next[now][i]=next[fail[now]][i];
  64. else
  65. {
  66. fail[next[now][i]]=next[fail[now]][i];
  67. Q.push(next[now][i]);
  68. }
  69. }
  70. }
  71. }
  72. int query(char buf[])
  73. {
  74. int len=strlen(buf);
  75. int res=0;
  76. int now=root;
  77. for(int i=0;i<len;i++)
  78. {
  79. now=next[now][buf[i]-'a'];
  80. int temp=now;
  81. while(temp != root)
  82. {
  83. res+=end[temp];
  84. end[temp]=0;
  85. temp=fail[temp];
  86. }
  87. }
  88. return res;
  89. }
  90. };
  91. char str[maxn*2];
  92. Trie ac;
  93. int main()
  94. {
  95. int T;
  96. int n;
  97. scanf("%d",&T);
  98. while(T--)
  99. {
  100. scanf("%d",&n);
  101. ac.init();
  102. for(int i=0;i<n;i++)
  103. {
  104. scanf("%s",str);
  105. ac.insert(str);
  106. }
  107. ac.build();
  108. scanf("%s",str);
  109. printf("%d\n",ac.query(str));
  110. }
  111. return 0;
  112. }

hdu2222--Keywords Search+AC自己主动机模板的更多相关文章

  1. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  2. HDU 2222 Keywords Search AC自己主动机入门题

    单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...

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

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

  4. AC自己主动机模板

    AC自己主动机模板-- /* * AC自己主动机模板 * 用法: * 1.init() : 初始化函数 * 2.insert(str) : 插入字符串函数 * 3.build() : 构建ac自己主动 ...

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

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

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

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

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

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

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

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

  9. HDU2222 Keywords Search [AC自动机模板]

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

随机推荐

  1. 洛谷 P4073 [WC2013]平面图

    #include<bits/stdc++.h> using namespace std; ; typedef long double LD; ; ); int dcmp(LD x){ret ...

  2. 第五讲:Fast RTL-level verification

    1.good code styles 2.+rad compile time switch  for compile 1.了解VCS 的架构  <===这方便了解不多 parser / even ...

  3. ()-servlet.xml中剥离出的hibernate.cfg.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  4. Android开发——后台获取用户点击位置坐标(可获取用户支付宝密码)

    1. getevent命令 我们首先是根据adb shell getevent命令获取到被点击位置的信息. 这里要说明的是,不同的手机手机获得的点击输出是不一样的.以我的真机为例,输出如下 本文原创, ...

  5. MindManager 设置默认Note字体大小

    工具栏 Design > Notes Theme > Default Font

  6. SpringCloud源码地址

    SpringCloud实战源代码 https://github.com/springcloud/spring-cloud-code.git

  7. xtu字符串 C. Marlon's String

    C. Marlon's String Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java ...

  8. NYOJ-673悟空的难题~~水题~~

    悟空的难题 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 自从悟空当上了齐天大圣,花果山上的猴子猴孙们便也可以尝到天上的各种仙果神酒,所以猴子猴孙们的体质也得到了很好的 ...

  9. [Istio]Kubernetes集群部署Istio 1.0

    大部分内容都是可以根据https://istio.io/docs/setup/kubernetes/quick-start/来处理的,这里主要谈部署时一些细节的问题 首先,当我们按照 istio 官方 ...

  10. GO 语言周报【七月第 1 期】

    TIOBE 七月排名 Go 进入前十 TIOBE 七月头条:Go 语言达到历史最高并进入前十.对于 Go 语言来说,这是一个里程碑时刻,我们可以更大胆地想象,它下一步的发展会达到怎样的高度.Go 是否 ...