Problem Description

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

Wiskey also wants to bring this feature to his image retrieval system.

Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.

To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.

Input

First line will contain one integer means how many cases will follow by.

Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)

Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.

The last line is the description, and the length will be not longer than 1000000.

Output

Print how many keywords are contained in the description.

Sample Input

1

5

she

he

say

shr

her

yasherhs

Sample Output

3

Description(CHN)

给 \(n\) 个模式串和一个文本串,求文本串中出现了多少个模式串

Solution

AC自动机裸题,做法跟【刷题】洛谷 P3808 【模板】AC自动机(简单版)一模一样

  1. #include<bits/stdc++.h>
  2. #define ui unsigned int
  3. #define ll long long
  4. #define db double
  5. #define ld long double
  6. #define ull unsigned long long
  7. const int MAXN=10000+10,MAXM=1000000+10,MAXS=500000+10;
  8. int T,n,ch[MAXS][30],fail[MAXS],ed[MAXS],cnt,vis[MAXS],last[MAXS],ps[MAXN];
  9. char s[MAXM];
  10. std::queue<int> q;
  11. template<typename T> inline void read(T &x)
  12. {
  13. T data=0,w=1;
  14. char ch=0;
  15. while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
  16. if(ch=='-')w=-1,ch=getchar();
  17. while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
  18. x=data*w;
  19. }
  20. template<typename T> inline void write(T x,char ch='\0')
  21. {
  22. if(x<0)putchar('-'),x=-x;
  23. if(x>9)write(x/10);
  24. putchar(x%10+'0');
  25. if(ch!='\0')putchar(ch);
  26. }
  27. template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
  28. template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
  29. template<typename T> inline T min(T x,T y){return x<y?x:y;}
  30. template<typename T> inline T max(T x,T y){return x>y?x:y;}
  31. inline void init()
  32. {
  33. for(register int i=0,lt=strlen(s);i<lt;++i)s[i]-='a'-1;
  34. }
  35. inline void insert(int rk)
  36. {
  37. int x=0;
  38. for(register int i=0,lt=strlen(s);i<lt;++i)
  39. if(!ch[x][s[i]])x=ch[x][s[i]]=++cnt;
  40. else x=ch[x][s[i]];
  41. ed[x]=1;
  42. ps[rk]=x;
  43. }
  44. inline void getfail()
  45. {
  46. for(register int i=1;i<=26;++i)
  47. if(ch[0][i])q.push(ch[0][i]);
  48. while(!q.empty())
  49. {
  50. int x=q.front();
  51. q.pop();
  52. for(register int i=1,y,z;i<=26;++i)
  53. if(ch[x][i])
  54. {
  55. y=ch[x][i],z=fail[x];
  56. while(z&&!ch[z][i])z=fail[z];
  57. fail[y]=ch[z][i];
  58. last[y]=ed[fail[y]]?fail[y]:last[fail[y]];
  59. q.push(y);
  60. }
  61. else ch[x][i]=ch[fail[x]][i];
  62. }
  63. }
  64. inline void save(int x)
  65. {
  66. if(x)
  67. {
  68. if(ed[x])vis[x]=1;
  69. save(last[x]);
  70. }
  71. }
  72. inline void match()
  73. {
  74. int x=0;
  75. for(register int i=0,lt=strlen(s);i<lt;++i)x=ch[x][s[i]],save(ed[x]||last[x]?x:0);
  76. }
  77. int main()
  78. {
  79. read(T);
  80. while(T--)
  81. {
  82. memset(ch,0,sizeof(ch));
  83. memset(ed,0,sizeof(ed));
  84. memset(ps,0,sizeof(ps));
  85. memset(vis,0,sizeof(vis));
  86. memset(fail,0,sizeof(fail));
  87. memset(last,0,sizeof(last));
  88. cnt=0;
  89. read(n);
  90. for(register int i=1;i<=n;++i)scanf("%s",s),init(),insert(i);
  91. getfail();
  92. scanf("%s",s);init();match();
  93. int ans=0;
  94. for(register int i=1;i<=n;++i)
  95. if(vis[ps[i]])ans++;
  96. write(ans,'\n');
  97. }
  98. return 0;
  99. }

【刷题】HDU 2222 Keywords Search的更多相关文章

  1. HDU 2222 Keywords Search(查询关键字)

    HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...

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

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

  3. hdu 2222 Keywords Search 模板题

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

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

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

  5. HDU 2222 Keywords Search(瞎搞)

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

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

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...

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

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

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

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

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

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

随机推荐

  1. 零基础学Python之结构化数据(附详细的代码解释和执行结果截图)

    3结构化数据 字典(查找表).集合.元组.列表 3.1字典 是有两列任意多行的表,第一列存储一个键,第二列存储一个值. 它存储键/值对,每个唯一的键有一个唯一与之关联的值.(类似于映射.表) 它不会维 ...

  2. Xavier——Understanding the difficulty of training deep feedforward neural networks

    1. 摘要 本文尝试解释为什么在深度的神经网络中随机初始化会让梯度下降表现很差,并且在此基础上来帮助设计更好的算法. 作者发现 sigmoid 函数不适合深度网络,在这种情况下,随机初始化参数会让较深 ...

  3. Windows本地上传源码到Gitee远程仓库

    1.下载Git,并安装. 安装时一路默认即可 https://git-scm.com/downloads 验证Git安装成功否 cmd 下输入,出现版本号即成功 git --version 2.生成s ...

  4. Vue+webpack项目中,运行报错Cannot find module 'chalk'的处理

    刚开始用vue + webpack新建项目,在github上下载了一个示例,输入npm init >>>npm run dev 后报错 Cannot find module 'cha ...

  5. Python函数初识二

    一.变量的作用域LEGB 1.1.变量的作用域 在Python中,程序的变量并不是在哪个位置都可以访问的,访问权限决定于这个变量是在哪里赋值的.变量的作用域决定了在哪一部分程序可以访问哪个特定的变量名 ...

  6. 随手记录-linux-添加epel源

    下载各种yum源 https://opsx.alibaba.com/mirror https://blog.csdn.net/harbor1981/article/details/51135623

  7. 20172324《Java程序设计》第3周学习总结

    20172324<Java程序设计>第3周学习总结 教材学习内容总结 随机数,记住要返回的是指定的字符前一个. String类型的一些用法,例如concat(连接),toUpperCase ...

  8. Alpha阶段项目Postmortem会议总结

    (一)设想和目标 1.我们的软件要解决什么问题?是否定义的很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件主要解决总是不知道在什么时间该做什么事情,或是老是忘记做一些事情的问题,通过添加事件 ...

  9. Merge join、Hash join、Nested loop join对比分析

    简介 我们所常见的表与表之间的Inner Join,Outer Join都会被执行引擎根据所选的列,数据上是否有索引,所选数据的选择性转化为Loop Join,Merge Join,Hash Join ...

  10. mvc学习-编辑提交需要注意-mvc重点

    示例代码: // GET: /Movies/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpSt ...