2222

思路:

  ac自动机模板题;

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. #define maxn 500005
  9.  
  10. struct TreeNodeType {
  11. int count;
  12.  
  13. TreeNodeType *fail;
  14.  
  15. TreeNodeType *next[];
  16.  
  17. TreeNodeType()
  18. {
  19. fail=NULL,count=;
  20. for(int i=;i<;i++) next[i]=NULL;
  21. }
  22. };
  23. struct TreeNodeType *root,*que[maxn];
  24.  
  25. int n;
  26.  
  27. char str[maxn*],word[];
  28.  
  29. inline void in(int &now)
  30. {
  31. char Cget=getchar();now=;
  32. while(Cget>''||Cget<'') Cget=getchar();
  33. while(Cget>=''&&Cget<='')
  34. {
  35. now=now*+Cget-'';
  36. Cget=getchar();
  37. }
  38. }
  39.  
  40. void insert(char *ch)
  41. {
  42. int temp,len=strlen(ch);
  43. TreeNodeType *p=root;
  44. for(int i=;i<len;i++)
  45. {
  46. temp=ch[i]-'a';
  47. if(p->next[temp]==NULL) p->next[temp]=new TreeNodeType;
  48. p=p->next[temp];
  49. }
  50. p->count++;
  51. }
  52.  
  53. void build()
  54. {
  55. int tail=,head=;que[head]=root;
  56. while(head<tail)
  57. {
  58. TreeNodeType *p=que[head++],*temp=NULL;
  59. for(int i=;i<;i++)
  60. {
  61. if(p->next[i]==NULL) continue;
  62. if(p==root) p->next[i]->fail=root;
  63. else
  64. {
  65. temp=p->fail;
  66. while(temp!=NULL)
  67. {
  68. if(temp->next[i]!=NULL)
  69. {
  70. p->next[i]->fail=temp->next[i];
  71. break;
  72. }
  73. temp=temp->fail;
  74. }
  75. if(temp==NULL) p->next[i]->fail=root;
  76. }
  77. que[tail++]=p->next[i];
  78. }
  79. }
  80. }
  81.  
  82. int query()
  83. {
  84. int pos,len=strlen(str),res=;TreeNodeType *p=root;
  85. for(int i=;i<len;i++)
  86. {
  87. pos=str[i]-'a';
  88. while(p->next[pos]==NULL&&p!=root) p=p->fail;
  89. if(p->next[pos]!=NULL) p=p->next[pos];else p=root;
  90. TreeNodeType *temp=p;
  91. while(temp!=root&&temp->count!=-) res+=temp->count,temp->count=-,temp=temp->fail;
  92. }
  93. return res;
  94. }
  95.  
  96. int main()
  97. {
  98. int T;
  99. in(T);
  100. while(T--)
  101. {
  102. root=new TreeNodeType;
  103. in(n);for(int i=;i<=n;i++) gets(word),insert(word);
  104. build(),scanf("%s",str),printf("%d\n",query());
  105. }
  106. return ;
  107. }

AC日记——Keywords Search hdu 2222的更多相关文章

  1. Keywords Search HDU - 2222 ( ac自动机)模版题

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

  2. Keywords Search HDU - 2222 AC自动机板子题

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

  3. Keywords Search - HDU 2222(AC自动机模板)

    题目大意:输入几个子串,然后输入一个母串,问在母串里面包含几个子串.   分析:刚学习的AC自动机,据说这是个最基础的模板题,所以也是用了最基本的写法来完成的,当然也借鉴了别人的代码思想,确实是个很神 ...

  4. Keywords Search HDU - 2222(ac自动机板题。。)

    求一个字符串上有多少个匹配的单词 看着卿学姐的板子写的 指针形式: #include <iostream> #include <cstdio> #include <sst ...

  5. AC自动机---Keywords Search

    题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110773#problem/A Description In the moder ...

  6. AC日记——Number Sequence hdu 1711

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. AC日记——统计难题 hdu 1251

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  8. AC日记——病毒侵袭 hdu 2896

    2896 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...

  9. AC日记——Paint Pearls hdu 5009

    Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <c ...

随机推荐

  1. [Java文件操作] 为文本文件添加行号

    [思路]将文件中的内容按行读取存入一个字符串中,在输出时再为每一行加上行号. import java.io.*; public class Text { private String strFinal ...

  2. 多线程 定时器 Timer TimerTask

    定时器是一种特殊的多线程,使用Timer来安排一次或者重复执行某个任务 package org.zln.thread; import java.util.Date; import java.util. ...

  3. group by 分组后 返回的是一个同属性的集合

    group by 分组后 返回的是一个同属性的集合  我们可以遍历该集合

  4. Codeforces Round #401 (Div. 1) C(set+树状数组)

    题意: 给出一个序列,给出一个k,要求给出一个划分方案,使得连续区间内不同的数不超过k个,问划分的最少区间个数,输出时将k=1~n的答案都输出 比赛的时候想的有点偏,然后写了个nlog^2n的做法,T ...

  5. MySQL 服务器安装及命令使用

    本文来自实验楼相关部分的文档和实验操作过程. 一.MySQL简介 MySQL是一种关系数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,提高了速度并提高了灵活性.My ...

  6. 【BZOJ 3123】 [Sdoi2013]森林 主席树启发式合并

    我们直接按父子关系建主席树,然后记录倍增方便以后求LCA,同时用并查集维护根节点,而且还要记录根节点对应的size,用来对其启发式合并,然后每当我们合并的时候我们都要暴力拆小的一部分重复以上部分,总时 ...

  7. 如何写出规范的JavaScript代码

    作为一名开发人员(WEB前端JavaScript开发),不规范的开发不仅使日后代码维护变的困难,同时也不利于团队的合作,通常还会带来代码安全以及执行效率上的问题.本人在开发工作中就曾与不按规范来开发的 ...

  8. 解决导出为Excel时文件名乱码的问题。

    以前代码:public static void htmlToExcel(HttpContext context, string title, string html, string fileCss = ...

  9. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) B

    B. Problems for Round time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  10. 问题总结——window平台下grunt\bower安装后无法运行的问题

    一.问题: 安装grunt或者bower后,在cmd控制台运行grunt -version 或者 bower -v会出现:“xxx不是内部或外部命令,也不是可运行的程序或批处理文件”,