• Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total
Submission(s):
39064    Accepted
Submission(s): 12596
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
 
Recommend

lcy

  1. 题意:看现在的主字符串,有多少个零碎的字符串构成,
  2. 做法:AC自动机模板
  3. 代码如下:
  4. #include<iostream>
  5. #include<string.h>
  6. #include<algorithm>
  7. #include<stdio.h>
  8. #include<cmath>
  9. #include<queue>
  10. #define maxn 500010
  11. using namespace std;
  12. struct Tire
  13. {
  14. int next[maxn][],fail[maxn],end[maxn];
  15. int root,L;
  16. int newnode()
  17. {
  18. for(int i=; i<; i++)
  19. {
  20. next[L][i] = -;
  21. }
  22. end[L++] = ;
  23. return L-;
  24. }
  25. void init()
  26. {
  27. L = ;
  28. root = newnode();
  29. }
  30. void insert(char buf[])
  31. {
  32. int len = strlen(buf);
  33. int now = root;
  34. for(int i=;i<len;i++)
  35. {
  36. if(next[now][buf[i] - 'a'] == -)
  37. {
  38. next[now][buf[i] -'a'] = newnode();
  39. }
  40. now = next[now][buf[i] -'a'];
  41. }
  42. end[now]++;
  43. }
  44. void build()
  45. {
  46. queue<int>Q;
  47. fail[root] = root;
  48. for(int i=;i<;i++)
  49. {
  50. if(next[root][i] == -)
  51. next[root][i] = root;
  52. else
  53. {
  54. fail[next[root][i]] = root;
  55. Q.push(next[root][i]);
  56. }
  57. }
  58. while(!Q.empty())
  59. {
  60. int now = Q.front();
  61. Q.pop();
  62. for(int i=;i<;i++)
  63. {
  64. if(next[now][i] == -)
  65. next[now][i] = next[fail[now]][i];
  66. else
  67. {
  68. fail[next[now][i]] = next[fail[now]][i];
  69. Q.push(next[now][i]);
  70. }
  71. }
  72. }
  73. }
  74. int query(char buf[])
  75. {
  76. int len = strlen(buf);
  77. int now = root;
  78. int res = ;
  79. for(int i=;i<len;i++)
  80. {
  81. now = next[now][buf[i] -'a'];
  82. int tmp = now;
  83. while(tmp != root)
  84. {
  85. res += end[tmp];
  86. end[tmp] = ;
  87. tmp = fail[tmp];
  88. }
  89. }
  90. return res;
  91. }
  92. };
  93. char buf[];
  94. Tire ac;
  95. int main()
  96. {
  97. #ifndef ONLINE_JUDGE
  98. freopen("in.txt","r",stdin);
  99. #endif // ONLINE_JUDGE
  100. int T;
  101. scanf("%d",&T);
  102. while(T--)
  103. {
  104. int n;
  105. scanf("%d",&n);
  106. ac.init();
  107. for(int i=;i<n;i++)
  108. {
  109. scanf("%s",buf);
  110. ac.insert(buf);
  111. }
  112. ac.build();
  113. scanf("%s",buf);
  114. printf("%d\n",ac.query(buf));
  115. }
  116. return ;
  117. }

HDU-2222的更多相关文章

  1. HDU 2222  AC自动机模板题

    1.HDU 2222 2.题意:给出n个单词,一个字串,求有多少个单词在字串里出现了.注意给出的单词可能会重复,重复的不计. 3.总结:入门题.在查询这里还是不太懂. #include<bits ...

  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(查询关键字)

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

  4. HDU 2222 最简单的AC自动机套模板应用

    HDU 2222 题意:给出N(N<=10,000)个单词,每个单词长度不超过50.再给出一个字符串S,字符串长度不超过1,000,000.问有多少个单词出现在了字符串S中.(单词可能重复,单词 ...

  5. HDU 2222 (AC自动机)

    HDU 2222 Keywords search Problem : 给若干个模式串,询问目标串中出现了多少个模式串. Solution : 复习了一下AC自动机.需要注意AC自动机中的fail,和n ...

  6. HDU - 2222,HDU - 2896,HDU - 3065,ZOJ - 3430 AC自动机求文本串和模式串信息(模板题)

    最近正在学AC自动机,按照惯例需要刷一套kuangbin的AC自动机专题巩固 在网上看过很多模板,感觉kuangbin大神的模板最为简洁,于是就选择了用kuangbin大神的模板. AC自动机其实就是 ...

  7. hdu 2222 Keywords Search

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

  8. HDU 2222 AC自动机模板题

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

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

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

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

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 KMP是单模式串匹配的算法,而AC自动机是用于多模式串匹配的算法.主要由Trie和KMP的思想构成. 题意 ...

随机推荐

  1. 如何在vi中设置tab的长度

    在使用vi写python时(其实,不管用什么写python程序时,都要注意),首先要将Tab键的长度设为4,因为使用python编程时,我们是通过缩进来实现作用域的,所以要统一Tab键的长度.具体方法 ...

  2. 洛谷 U14472 数据结构【比赛】 【差分数组 + 前缀和】

    题目描述 蒟蒻Edt把这个问题交给了你 ---- 一个精通数据结构的大犇,由于是第一题,这个题没那么难.. edt 现在对于题目进行了如下的简化: 最开始的数组每个元素都是0 给出nnn,optopt ...

  3. SpringBoot web 小项目

    Spring Boot 整合 Thymeleaf 完整 Web 案例 原创出处  作者:泥瓦匠BYSocket 希望转载,保留摘要,谢谢! Thymeleaf 是一种模板语言.那模板语言或模板引擎是什 ...

  4. [Oracle整理]ORA-12705(字符集问题)

    [Oracle整理]ORA-12705(字符集问题)   2017年5月11日 18:11 [Oracle整理]ORA-12705(字符集问题) 说明:本内容是工作用到的知识点整理,来自工作中和网络. ...

  5. thinkphp 5 where 组合条件map数组or

    if($inviterId>0) { $arr = Db::table("tablename")-> where("pid=$inviterId") ...

  6. conda 虚拟环境

    一.jupyter notbook (1)需要安装: conda install ipykernel (2)首先激活对应的conda环境 source activate 环境名称 (3)将环境写入no ...

  7. Git同时push到多个远程仓库

    添加第二个远程地址时使用以下命令: git remote set-url --add origin git@github.com:morethink/programming.git 查看远程分支:gi ...

  8. Shiro实战教程(二)

    http://www.jianshu.com/p/6786ddf54582/ https://www.cnblogs.com/ealenxie/p/10610741.html

  9. IFeatureCursorProxy.flush AutomationException: 0x80041538

    添加面的时候碰到的一个问题,有些数据没问题,有些报错,后来请教一位同事说有可能是经纬度字段的数据精度问题,因为投影坐标系统不同,支持的经纬度经度不同,后来转换投影坐标系统后果然解决问题了,我一开始也怀 ...

  10. ADO.NET中带参数的Sql语句的陷阱

    1.使用Parameter //利用构造函数方式 ,不推荐这样写 Parameter p =new Parameter("@id",值); cmd.Parameters.Add(p ...