题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2896

题目大意:多个模式串。多个匹配串。其中串的字符范围是(0~127)。问匹配串中含有哪几个模式串。

解题思路

AC自动机模板题。注意一下字符范围。

cnt记录这个模式串的个数改为这个模式串的index。

find的时候,把找到的index压入vector里面即可。

注意有多个匹配串,每次find之后会把last->cnt修改,原因是防止一个模式串出现了多次被压入vector,所以先备份一下,再还原回来。

  1. #include "cstdio"
  2. #include "cstring"
  3. #include "string"
  4. #include "iostream"
  5. #include "queue"
  6. #include "vector"
  7. #include "algorithm"
  8. using namespace std;
  9. #define maxn 130
  10. struct Trie
  11. {
  12. Trie *next[maxn],*fail;
  13. int cnt;
  14. }*root;
  15. struct status
  16. {
  17. Trie *last;
  18. int cnt;
  19. status(Trie *last,int cnt):last(last),cnt(cnt) {}
  20. };
  21. Trie *newnode()
  22. {
  23. Trie *ret=new Trie;
  24. memset(ret->next,,sizeof(ret->next));
  25. ret->fail=;
  26. ret->cnt=;
  27. return ret;
  28. }
  29. void init() {root=newnode();}
  30. void Insert(string str,int index)
  31. {
  32. Trie *pos=root;
  33. for(int i=;i<str.size();i++)
  34. {
  35. int c=str[i];
  36. if(!pos->next[c]) pos->next[c]=newnode();
  37. pos=pos->next[c];
  38. }
  39. pos->cnt=index;
  40. }
  41. void getfail()
  42. {
  43. queue<Trie *> Q;
  44. for(int c=;c<maxn;c++)
  45. {
  46. if(root->next[c])
  47. {
  48. root->next[c]->fail=root;
  49. Q.push(root->next[c]);
  50. }
  51. else root->next[c]=root;
  52. }
  53. while(!Q.empty())
  54. {
  55. Trie *x=Q.front();Q.pop();
  56. for(int c=;c<maxn;c++)
  57. {
  58. if(x->next[c])
  59. {
  60. x->next[c]->fail=x->fail->next[c];
  61. Q.push(x->next[c]);
  62. }
  63. else x->next[c]=x->fail->next[c];
  64. }
  65. }
  66. }
  67. vector<int> find(string str)
  68. {
  69. Trie *pos=root,*last;
  70. queue<status> Q;
  71. vector<int> ans;
  72. for(int i=;i<str.size();i++)
  73. {
  74. int c=str[i];last;
  75. if(pos->next[c])
  76. {
  77. pos=pos->next[c];
  78. last=pos;
  79. while(last->cnt)
  80. {
  81. Q.push(status(last,last->cnt));
  82. ans.push_back(last->cnt);
  83. last->cnt=; //修改last->cnt
  84. last=last->fail;
  85. }
  86. }
  87. }
  88. while(!Q.empty()) //恢复last->cnt
  89. {
  90. status x=Q.front();Q.pop();
  91. x.last->cnt=x.cnt;
  92. }
  93. return ans;
  94. }
  95. int main()
  96. {
  97. //freopen("in.txt","r",stdin);
  98. ios::sync_with_stdio(false);
  99. int n,m;
  100. string tt;
  101. while(cin>>n)
  102. {
  103. init();
  104. for(int i=;i<=n;i++)
  105. {
  106. cin>>tt;
  107. Insert(tt,i);
  108. }
  109. getfail();
  110. cin>>m;
  111. int cnt=;
  112. for(int i=;i<=m;i++)
  113. {
  114. cin>>tt;
  115. vector<int> ans=find(tt);
  116. sort(ans.begin(),ans.end());
  117. if(!ans.size()) continue;
  118. cnt++;
  119. printf("web %d:",i);
  120. for(int j=;j<ans.size();j++) printf(" %d",ans[j]);
  121. printf("\n");
  122. }
  123. printf("total: %d\n",cnt);
  124. }
  125. }
2871595 neopenx HDU 2896 Accepted 29988 343 C++ 2565
9 min ago

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

  1. HDU 2222 AC自动机模板题

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

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

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

  3. hdu 2896 AC自动机模版题

    题意:输出出现模式串的id,还是用end记录id就可以了. 本题有个关键点:“以上字符串中字符都是ASCII码可见字符(不包括回车).”  -----也就说AC自动机的Trie树需要128个单词分支. ...

  4. HDU 2896 AC自动机 裸题

    中文题题意不再赘述 注意字符范围是可见字符,从32开始到95 char c - 32 #include <stdio.h> #include <string.h> #inclu ...

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

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

  6. hdu 2896 AC自动机

    // hdu 2896 AC自动机 // // 题目大意: // // 给你n个短串,然后给你q串长字符串,要求每个长字符串中 // 是否出现短串,出现的短串各是什么 // // 解题思路: // / ...

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

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

  8. HDu-2896 病毒侵袭,AC自动机模板题!

    病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...

  9. [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]

    AC自动机模板题(膜jcvb代码) #include <iostream> #include <algorithm> #include <cstdio> #incl ...

随机推荐

  1. Instance Variables in ruby

    Dogs have many shared characteristics, like the abilities to wag their tails and drink water from a ...

  2. puppet之自定义fact(转载)

    1.使用环境变量'FACTERLIB'创建fact 1.1.在自定义目录里面定义一个fact,列出当前系统登录的用户数 [root@agent1 ~]# vim /var/lib/puppet/kis ...

  3. apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104))

    apache ab压力测试报错(apr_socket_recv: Connection reset by peer (104))   今天用apache 自带的ab工具测试,当并发量达到1000多的时 ...

  4. PHP编译支持mysqli

    PHP编译支持mysqli前提是必须安装mysql直接上命令先进入源码包我的源码包是在/usr/local/php-5.2.1/ext/mysqli这样进入 cd /usr/local/php-5.2 ...

  5. shell脚本的调试技巧

    请参考文章:http://www.ibm.com/developerworks/cn/linux/l-cn-shell-debug/index.html 读后的感觉,还是用shell的选项灵活,方便. ...

  6. Continuous Subarray Sum

    Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...

  7. 【USACO】checker

    一看题目 经典的8皇后问题 不过是皇后数量可变而已 不用想 回溯法. 需要个生成每次可选择序列的函数, 在存储可选择的序列时按照先大后小的顺序排的.这样每次找最小和去掉最小都很方便,只要有个记录数量的 ...

  8. CodeForces - 405C

    Unusual Product Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Sub ...

  9. Servlet、JSP选择题(2)

    Java EE软件工程师认证考试 试题库-选择题 一.    选择题(包括单选和双选) 1.B 编写一个Filter,需要(  ) A. 继承Filter 类 B. 实现Filter 接口 C. 继承 ...

  10. php 克隆和引用类

    /*class Ren { public $name; public $sex; function __construct($n,$s) { $this->name=$n; $this-> ...