题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数)(如果单词x在字符串中出现一次而在单词表中有两个则ans+2,在字符串中出现两次而单词表中有一个则ans+1)
AC自动机模板题,之前学了总觉得不扎实,现在有底了现在终于可以去敲心头大恨,兴奋!!!
代码
  1. #include<cstdio>
  2. #include<cstring>
  3. const int maxn=;
  4. const double eps=1e-;
  5. const long long modn=;
  6. int n;
  7. char a[]={};
  8. char b[*maxn]={};
  9. struct trie{
  10. int next[];
  11. bool exist;
  12. int count;
  13. int fail;
  14. }e[maxn*]={};
  15. int tot,ans;
  16. int q[maxn*]={};
  17.  
  18. void init(int x,int k,int j){
  19. if(j>k){
  20. e[x].exist=;
  21. e[x].count++;
  22. return;
  23. }
  24. int z=a[j]-'a';
  25. if(!e[x].next[z])
  26. e[x].next[z]=++tot;
  27. init(e[x].next[z],k,j+);
  28. }
  29. void fir(){
  30. memset(e,,sizeof(e)); memset(q,,sizeof(q));
  31. tot=ans=;
  32. }
  33. void doit(){
  34. int head=,tail=,x,y,f;
  35. q[]=;
  36. while(head<=tail){
  37. x=q[head++];
  38. for(int i=;i<;i++){
  39. if(e[x].next[i]){
  40. y=e[x].next[i];
  41. if(x!=){
  42. f=e[x].fail;
  43. while((!e[f].next[i]) && f )
  44. f=e[f].fail;
  45. e[y].fail=e[f].next[i];
  46. }q[++tail]=y;
  47. }
  48. }
  49. }
  50. }
  51. void find(){
  52. scanf("%s",&b);
  53. int k=std::strlen(b);
  54. int x=,z,y;
  55. for(int i=;i<k;i++){
  56. z=b[i]-'a';
  57. while( (!e[x].next[z])&& x){
  58. x=e[x].fail;
  59. }x=e[x].next[z]; y=x;
  60. while(y&&e[y].count){
  61. ans+=e[y].count;
  62. e[y].count=;
  63. y=e[y].fail;
  64. }
  65. }
  66. }
  67. int main(){
  68. int T;scanf("%d",&T);
  69. while(T-->){
  70. fir();
  71. scanf("%d",&n);
  72. for(int i=;i<=n;i++){
  73. scanf("%s",&a);
  74. init(,std::strlen(a)-,);
  75. }
  76. doit();
  77. find();
  78. printf("%d\n",ans);
  79. }
  80. return ;
  81. }

更新:

整理模板的时候发现自己原来的代码有问题。这个新版本应该没问题了。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<queue>
  7. using namespace std;
  8. const int maxn=;
  9. int n,siz;
  10. char ch[]={};
  11. char str[maxn*]={};
  12. struct trie{
  13. int sig[];
  14. int cnt;
  15. int vis;
  16. int fail;
  17. }t[maxn*];int tot=;
  18. int q[maxn*]={};int head=,tail=;
  19. void fir(){
  20. tot=;memset(t,,sizeof(t));//memset(q,0,sizeof(q));
  21. }
  22. void init(int x,int j){
  23. if(j>siz-){ t[x].cnt++; return; }
  24. int z=ch[j]-'a';
  25. if(!t[x].sig[z])t[x].sig[z]=++tot;
  26. init(t[x].sig[z],j+);
  27. }
  28. void build_fail(){
  29. int head=,tail=,x,y,f;q[]=;
  30. while(head<=tail){
  31. x=q[head++];
  32. for(int i=;i<;i++)
  33. if(t[x].sig[i]){
  34. y=t[x].sig[i];
  35. if(x){
  36. f=t[x].fail;
  37. while((!t[f].sig[i])&&f)
  38. f=t[f].fail;
  39. t[y].fail=t[f].sig[i];
  40. }q[++tail]=y;
  41. }
  42. }
  43. }
  44. int get_num(){//字符串中包含的单词数量,重复的不记录,
  45. //如果单词x在字符串中出现一次而在单词表中有两个则ans+2
  46. //在字符串中出现两次而单词表中有一个则ans+1
  47. int ans=,x=,y,z;
  48. for(int i=;i<siz;i++){
  49. z=str[i]-'a';
  50. while((!t[x].sig[z])&&x)
  51. x=t[x].fail;
  52. x=t[x].sig[z];y=x;
  53. while(y&&(!t[y].vis)){//保证了每个结尾只访问一次
  54. ans+=t[y].cnt;
  55. t[y].vis=;t[y].cnt=;
  56. y=t[y].fail;
  57. }
  58. }
  59. return ans;
  60. }
  61. int main(){
  62. int T;scanf("%d",&T);
  63. while(T-->){
  64. fir();
  65. scanf("%d",&n);
  66. for(int i=;i<=n;i++){scanf("%s",ch);siz=strlen(ch);init(,);}
  67. build_fail();
  68. scanf("%s",str);siz=strlen(str);
  69. printf("%d\n",get_num());
  70. }
  71. return ;
  72. }

new

POJ2222 Keywords Search AC自动机模板的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...

  9. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

随机推荐

  1. MQTT协议及推送服务

    MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的“轻量级”通讯协议,该协议构建 ...

  2. 用体渲染的方法在Unity中渲染云(18/4/4更新)

    github: https://github.com/yangrc1234/VolumeCloud 更新的内容在底部 最近在知乎上看到一篇文章讲云层的渲染(https://zhuanlan.zhihu ...

  3. 在Unity中实现屏幕空间反射Screen Space Reflection(4)

    第四部分讲一下如何在2D屏幕空间步进光线. http://casual-effects.blogspot.com/2014/08/screen-space-ray-tracing.html 中的代码感 ...

  4. HDU 1172 猜数字 (模拟)

    题目链接 Problem Description 猜数字游戏是gameboy最喜欢的游戏之一.游戏的规则是这样的:计算机随机产生一个四位数,然后玩家猜这个四位数是什么.每猜一个数,计算机都会告诉玩家猜 ...

  5. C++ STL标准入门

    C++:STL标准入门汇总 第一部分:(参考百度百科) 一.STL简介 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.它是由Alexand ...

  6. CentOS 6.6下目录结构及其主要作用

    今天我们总结一下CentOS 6.6的linux的目录结构,一个系统的目录众多,这里我们主要认识一下,根目录下的主要目录,首先我们可以通过tree命令查看一次根目录下一层目录都有什么目录, 补充:不能 ...

  7. 设计模式之笔记--外观模式(Facade)

    外观模式(Facade) 定义 外观模式(Facade),为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 类图 描述 Facade:外观类,外观 ...

  8. 002利用zabbix监控某个目录大小

    近期,因为JMS的消息堆积导致ApacheMQ频率故障(消息没有被消费掉,导致其数据库达到1.2G,JMS此时直接挂掉),很是郁闷!刚好自 己在研究zabbix.既然zabbix如此强大,那么它可以监 ...

  9. angular项目中使用angular-material2

    Step 1: Install Angular Material and Angular CDK npm install --save @angular/material @angular/cdk n ...

  10. PHP扩展插件 imagick 、PDO_MYSQL 安装

    环境准备 echo $LC_ALL echo "export LC_ALL=C" >> /etc/profile source /etc/profile yum ins ...