Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 50451    Accepted Submission(s): 16236

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
 
Author
Wiskey
  1. /* ***********************************************
  2. Author :guanjun
  3. Created Time :2016/5/26 22:17:48
  4. File Name :2222.cpp
  5. ************************************************ */
  6. #include <iostream>
  7. #include <cstring>
  8. #include <cstdlib>
  9. #include <stdio.h>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <queue>
  13. #include <set>
  14. #include <map>
  15. #include <string>
  16. #include <math.h>
  17. #include <stdlib.h>
  18. #include <iomanip>
  19. #include <list>
  20. #include <deque>
  21. #include <stack>
  22. #define ull unsigned long long
  23. #define ll long ling
  24. #define mod 90001
  25. #define INF 0x3f3f3f3f
  26. #define maxn 250010
  27. #define cle(a) memset(a,0,sizeof(a))
  28. const ull inf = 1LL << ;
  29. const double eps=1e-;
  30. using namespace std;
  31.  
  32. struct ACauto{
  33. int ch[maxn][];
  34. int sz;
  35. int f[maxn],last[maxn],val[maxn],cnt[maxn];
  36. void init(){
  37. sz=;
  38. memset(ch[],,sizeof ch[]);
  39. memset(cnt,,sizeof cnt);
  40. }
  41. int idx(char c){
  42. return c-'a';
  43. }
  44. void add(char *s,int v){
  45. int u=,len=strlen(s);
  46. for(int i=;i<len;i++){
  47. int c=idx(s[i]);
  48. if(!ch[u][c]){
  49. memset(ch[sz],,sizeof ch[sz]);
  50. val[sz]=;
  51. ch[u][c]=sz++;
  52. }
  53. u=ch[u][c];
  54. }
  55. val[u]=v;
  56. }
  57. void getfail(){
  58. queue<int>q;
  59. f[]=;
  60. for(int c=;c<;c++){
  61. int u=ch[][c];
  62. if(u){
  63. f[u]=;
  64. q.push(u);
  65. last[u]=;
  66. }
  67. }
  68. while(!q.empty()){
  69. int r=q.front();q.pop();
  70. for(int c=;c<;c++){
  71. int u=ch[r][c];
  72. if(!u){
  73. ch[r][c]=ch[f[r]][c];
  74. continue;
  75. }
  76. q.push(u);
  77. f[u]=ch[f[r]][c];
  78. last[u]=val[f[u]]?f[u]:last[f[u]];
  79. }
  80. }
  81. }
  82. void print(int j){
  83. if(j){
  84. cnt[val[j]]++;
  85. print(last[j]);
  86. }
  87. }
  88. void Find(char *T){
  89. int n=strlen(T);
  90. int j=;
  91. for(int i=;i<n;i++){
  92. int c=idx(T[i]);
  93. while(j&&!ch[j][c])j=f[j];
  94. j=ch[j][c];
  95. if(val[j])print(j);
  96. else if(last[j])print(last[j]);
  97. }
  98. }
  99. }ac;
  100. char s[][],T[];
  101. int BKDRHash(char* s)
  102. {
  103. long long seed=;
  104. long long hash=;
  105. while(*s=='')s++;
  106. while(*s)
  107. {
  108. hash=hash*seed+(*s++);
  109. }
  110. return (hash & 0x7FFFFFFF);
  111. }
  112. map<int ,int>mp;
  113. int main()
  114. {
  115. #ifndef ONLINE_JUDGE
  116. freopen("in.txt","r",stdin);
  117. #endif
  118. //freopen("out.txt","w",stdout);
  119. int t,n;
  120. cin>>t;
  121. while(t--){
  122. ac.init();
  123. mp.clear();
  124. scanf("%d",&n);
  125. for(int i=;i<=n;i++){
  126. scanf("%s",s[i]);
  127. ac.add(s[i],i);
  128. int x=BKDRHash(s[i]);
  129. mp[x]++;
  130. }
  131. ac.getfail();
  132. scanf("%s",T);
  133. ac.Find(T);
  134. int ans=;
  135. for(int i=;i<=n;i++){
  136. int x=ac.cnt[i];
  137. int y=BKDRHash(s[i]);
  138. if(x>){
  139. ans+=mp[y];
  140. }
  141. }
  142. printf("%d\n",ans);
  143. }
  144. return ;
  145. }

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 ac自己主动机

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

  4. hdu 2222 Keywords Search 模板题

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

  5. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  6. hdu 2222 Keywords Search

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:裸AC自动机,直接贴代码做模板 #include<stdio.h> #includ ...

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

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

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

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

  9. 【刷题】HDU 2222 Keywords Search

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

随机推荐

  1. bzoj 5055: 膜法师 树状数组+离散

    先枚举每一个数,看它前面有几个比它小,算一下和为sum1,后面有几个比它大,算一下和为sum2,对答案的贡献为A[i]*sum1*sum2. 离散化后,树状数组就可以了. 就是倒着一边,顺着一边,统计 ...

  2. Spring入门之setter DI注入

    1.新建Java项目导入依赖jar包,参考前一章 2.以不同文件格式输出为例 3.定义接口IOutputGenerator.java package com.spring.output; public ...

  3. MySQL实现了四种通信协议

    原文链接:http://blog.csdn.net/yangling132/article/details/50932705[侵删] TCP/IP协议,通常我们通过来连接MySQL,各种主要编程语言都 ...

  4. T2602 最短路径问题 codevs

    http://codevs.cn/problem/2602/ 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold   题目描述 Description 平面上有n个点(n& ...

  5. File类 递归 获取目录下所有文件文件夹

    package com.xiwi; import java.io.*; import java.util.*; class file{ public static void main(String a ...

  6. 2019年春招Android方向腾讯电话面试

    第一问:TCP与UDP的区别 参考答案: 1.基于连接与无连接 2.TCP要求系统资源较多,UDP较少: 3.UDP程序结构较简单 4.流模式(TCP)与数据报模式(UDP); 5.TCP保证数据正确 ...

  7. Linux下sh/bash/source/.命令的区别(转)

    一..sh文件介绍 .sh为Linux的脚本文件,我们可以通过.sh执行一些命令,可以理解为windows的.bat批处理文件. 二.点命令(.) .命令和source是同一个命令,可以理解为sour ...

  8. iOS开发 当前时间 时间戳 转换

    1.今天在做一个webservice的接口的时候,被要求传一个时间戳过去,然后就是开始在Google上找 2.遇到两个问题,一,当前时间转化为时间戳,二,获取的当前时间和系统的时间相差8个小时 一,转 ...

  9. 【Todo】RTP/RTCP/RTSP/SIP/SDP 等多媒体传输和会话协议

    参考 http://m.blog.csdn.net/article/details?id=6211447

  10. android studio 使用(一)

    官方网址入门:https://developer.android.com/studio/install.html 看下Build Your first App,