题目大意:统计一共出现了多少次模板串。

题目分析:AC自动机的模板题。不过这题有坑,相同的模板串不能只算一次。

代码如下:

  1. # include<iostream>
  2. # include<cstdio>
  3. # include<queue>
  4. # include<map>
  5. # include<string>
  6. # include<cstring>
  7. # include<algorithm>
  8. using namespace std;
  9.  
  10. const int N=10000;
  11.  
  12. int ch[N*50+5][26];
  13. int val[N*50+5];
  14. int last[N*50+5];
  15. int f[N*50+5];
  16. bool mark[N*50+5];
  17. map<string,int>mp;
  18. string str[N+5];
  19.  
  20. struct AC
  21. {
  22. int cnt,res;
  23. void init()
  24. {
  25. res=cnt=0;
  26. memset(ch,0,sizeof(ch));
  27. memset(val,0,sizeof(val));
  28. memset(mark,false,sizeof(mark));
  29. }
  30. int idx(char c)
  31. {
  32. return c-'a';
  33. }
  34. void insert(string str,int v)
  35. {
  36. int u=0;
  37. int n=str.size();
  38. for(int i=0;i<n;++i){
  39. int c=idx(str[i]);
  40. if(!ch[u][c]) ch[u][c]=++cnt;
  41. u=ch[u][c];
  42. }
  43. val[u]=v;
  44. }
  45. void getFail()
  46. {
  47. queue<int>q;
  48. f[0]=0;
  49. for(int i=0;i<26;++i){
  50. int u=ch[0][i];
  51. if(!u) continue;
  52. f[u]=0;
  53. q.push(u);
  54. last[u]=0;
  55. }
  56. while(!q.empty())
  57. {
  58. int r=q.front();
  59. q.pop();
  60. for(int i=0;i<26;++i){
  61. int u=ch[r][i];
  62. if(!u){
  63. ch[r][i]=ch[f[r]][i];
  64. }else{
  65. q.push(u);
  66. int v=f[r];
  67. while(v&&!ch[v][i]) v=f[v];
  68. f[u]=ch[v][i];
  69. last[u]=val[f[u]]?f[u]:last[f[u]];
  70. }
  71. }
  72. }
  73. }
  74. void ac(string str)
  75. {
  76. int n=str.size();
  77. int j=0;
  78. for(int i=0;i<n;++i){
  79. int c=idx(str[i]);
  80. j=ch[j][c];
  81. if(val[j]) print(j);
  82. else if(last[j]) print(last[j]);
  83. }
  84. }
  85. void print(int u)
  86. {
  87. if(u){
  88. if(!mark[val[u]]) res+=mp[str[val[u]]];
  89. mark[val[u]]=true;
  90. print(last[u]);
  91. }
  92. }
  93. int getResult()
  94. {
  95. return res;
  96. }
  97. }ac;
  98.  
  99. char s[N*100+5];
  100.  
  101. int main()
  102. {
  103. int T,n;
  104. scanf("%d",&T);
  105. while(T--)
  106. {
  107. mp.clear();
  108. scanf("%d",&n);
  109. ac.init();
  110. for(int i=1;i<=n;++i){
  111. cin>>str[i];
  112. ++mp[str[i]];
  113. ac.insert(str[i],i);
  114. }
  115. scanf("%s",s);
  116. ac.getFail();
  117. ac.ac(s);
  118. printf("%d\n",ac.getResult());
  119. }
  120. return 0;
  121. }

  

HDU-2222 Keywords Search(AC自动机--模板题)的更多相关文章

  1. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

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

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

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

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

  4. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  5. hdu 2222 Keywords Search——AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...

  6. HDU 2222 Keywords Search (AC自动机)

    题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...

  7. 【HDU 2222】Keywords Search AC自动机模板题

    参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...

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

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

  9. POJ2222 Keywords Search AC自动机模板

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

  10. Keywords Search(AC自动机模板)

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

随机推荐

  1. VB.NET vs. C#

    VB.NET Program Structure C# Imports System Namespace Hello    Class HelloWorld        Overloads Shar ...

  2. python的class的__str__()和__repr__()函数

    repr(object) 返回一个可以用来表示对象的可打印字符串首先,尝试生成这样一个字符串,将其传给 eval()可重新生成同样的对象 否则,生成用尖括号包住的字符串,包含类型名和额外的信息(比如地 ...

  3. Shell终端配置

    Shell终端配置 How to: Change / Setup bash custom prompt (PS1) 参考链接:https://www.cyberciti.biz/tips/howto- ...

  4. Shell 语法之结构化命令(流程控制)

    许多程序在脚本命令之间需要某种逻辑流控制,允许脚本根据变量值的条件或者其他命令的结果路过一些命令或者循环执行这些命令.这些命令通常被称为结构化命令.和其他高级程序设计语言一样,shell提供了用来控制 ...

  5. centos7 禁止防火墙

    #CentOS .0默认使用的是firewall作为防火墙,这里改为iptables防火墙. #firewall: systemctl start firewalld.service#启动firewa ...

  6. knn原理与实践

    knn法是一种基本分类与回归方法 应用:knn算法不仅可以用于分类,还可以用于回归.. 1.文本分类:文本分类主要应用于信息检索,机器翻译,自动文摘,信息过滤,邮件分类等任务. 2.可以使用knn算法 ...

  7. Yii 验证输入框是否输入的是数字

    在对应的Model文件的rules中加入如下代码: array('age,phone', 'numerical', 'integerOnly'=>true,'message'=>'{att ...

  8. Add two numbers [LeetCode]

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  9. centos下整合PagerDuty、nagios初探(on-call尝鲜和体验)

    [前言] 今天在某个群里看见有人介绍了PagerDuty,介绍到了slack.整合后可以更加方便和团队合作.于是我觉得来尝尝鲜. [PagerDuty是什么?] PagerDuty是一款能够在服务器出 ...

  10. linux下使用shell查看apache IP访问量

    1.查看TCP连接状态 netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn netstat -n | awk '/^tcp/ {++S[$NF]} ...