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

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
题解:
  用AC自动机判断一个字符串中有几次出现了上面给的单词。
  AC自动机的讲解:http://blog.csdn.net/mobius_strip/article/details/22549517
           http://blog.csdn.net/niushuai666/article/details/7002736
           http://www.cnblogs.com/kuangbin/p/3164106.html
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<algorithm>
 4 #include<string.h>
 5 #include<queue>
 6 using namespace std;
 7 struct Trie{
 8     int next[500010][26],end[500010];//存储节点信息:
 9     //next[i][j]=k 表示第 i个节点,在第 j个字母的位置上有个节点 k
10     //end[i]=k 表示以 i节点结尾的单词有 k个
11     int fail[500010];
12     int root,tot;//root表示根 tot表示总节点数
13     int newnode(){//建立新节点
14         for(int i=0;i<26;i++) next[tot][i]=-1;//新节点的儿子都是空
15         end[tot++]=0;//下一个新节点的编号
16         return tot-1;//返回当前新节点的编号
17     }
18     void init(){//初始化,建立Trie的入口
19         tot=0;
20         root=newnode();
21     }
22     void insert(char buf[]){//插入单词
23         int len=strlen(buf);
24         int now=root;//now表示应当插入位置的父亲
25         for(int i=0;i<len;i++){//算上原点,新单词插入深度为 1 ~ len
26             if(next[now][buf[i]-'a']==-1) next[now][buf[i]-'a']=newnode();
27             now=next[now][buf[i]-'a'];
28         }
29         end[now]++;//以该节点为结尾的单词数量+1
30     }
31     void build(){//建立fail指针
32         queue<int> Q;
33         fail[root]=root;//根的fail指向根
34         for(int i=0;i<26;i++){
35             if(next[root][i]==-1)//如果根的某些孩子为空,就把这些孩子看做根
36                 next[root][i]=root;
37             else{//如果不为空,这些孩子的fail肯定指向根,并加入队列
38                 fail[next[root][i]]=root;
39                 Q.push(next[root][i]);
40             }
41         }
42         while(!Q.empty()){
43             int now=Q.front(); Q.pop();
44             for(int i=0;i<26;i++){//利用已经确定fail指针的节点来更新
45                 if(next[now][i]==-1)//now的某孩子为空,让这个孩子的编号改为 now的fail指向的节点的孩子,依次类推,如果都没子节点就会指向根
46                     next[now][i]=next[fail[now]][i];
47                 else{
48                     fail[next[now][i]]=next[fail[now]][i];
49                     Q.push(next[now][i]);
50                 }
51             }
52         }
53     }
54     int query(char buf[]){//询问该串中出现了几个单词
55         int len=strlen(buf),now=root;
56         int res=0;
57         for(int i=0;i<len;i++){
58             now=next[now][buf[i]-'a'];
59             int temp=now;
60             while(temp!=root){
61                 res+=end[temp];
62                 end[temp]=0;
63                 temp=fail[temp];
64             }
65         }
66         return res;
67     }
68 }ac;
69 char buf[1000010];
70 int T,n;
71 int main(){
72     scanf("%d",&T);
73     while(T--){
74         scanf("%d",&n);
75         ac.init();
76         for(int i=0;i<n;i++){
77             scanf("%s",buf);
78             ac.insert(buf);
79         }
80         ac.build();
81         scanf("%s",buf);
82         printf("%d\n",ac.query(buf));
83     }
84     return 0;
85 }

转载于:https://www.cnblogs.com/CXCXCXC/p/5170703.html

hdu 2222:Keywords Search相关推荐

  1. HDU 2222 Keywords Search【ACAM】

    HDU 2222 Kerwords Search 代码风格模仿自:USETC每周算法讲解,AC自动机,郭老师! 输入一个T,对于每一个T给你一个n,接下来输入P个模式串,然后给你一个L串,L中出现了多 ...

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

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

  3. hdu 2222 Keywords Search(ac自动机)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给你一系列子串,再给你一个主串问你主串一共有几个匹配子串 原来使用字典树写的但数据有点大T ...

  4. hdu 2222 Keywords Search AC自动机——多串匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意: 给出n个单词,再给出一段包含m个字符的文章,求有多少个单词在文章里出现过. 思路: 才开开始以为简 ...

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

    一组数据: 1 3 sss sss sss sss ans:3 #include <cstdio> #include <cstdlib> #include <vector ...

  6. HDU - 2222 Keywords Search(AC自动机)

    题目链接:点击查看 题目大意:给出n个长度不超过50的模式串,再给出一个长度不超过1e6的主串,问模式串在主串中出现过多少次 题目分析:一开始我以为是求n次KMP,后来才知道这样做的时间复杂度是O(L ...

  7. HDU 2222 Keywords Search

    HDU_2222 今天开始学AC自动机了,这个就是我AC自动机的处女作了.这个题有个小trick就是单词列表中可能有重复的单词,但这些重复的单词应看做不同的,因此建字典树时做标记的时候,把原来的赋值为 ...

  8. 【AC自动机】HDU 2222 Keywords Search 裸题

    题意:给出的n个单词,出现在T中的个数. #include <stdio.h> #include <string.h> #include <stdlib.h> #i ...

  9. AC自动机(HDU 2222: Keywords Search)

    题意: 输入n个单词,再输入一篇文章,判断有多少个单词在文章中出现过 http://blog.csdn.net/niushuai666/article/details/7002823 注释都在代码里 ...

最新文章

  1. 怎样的架构设计能力,才能成功拿下阿里P7?
  2. 关于美工ps出图table格式的处理
  3. Hibernate的四种典型例子(增删改查)
  4. [设计模式篇]工厂模式和抽象工厂模式
  5. 本体开发方法——the Method of Ontology Development
  6. 有效利用时间12妙招
  7. 语言程序推箱子课设报告_“延期不延学”第13期 | C++篇 | c++课设建议
  8. 104.202.60.2/.index.php,web扫描
  9. 医学实验室质量和能力认可准则在实验室信息系统的应用说明CNAS-CL35
  10. UNIX高级环境编程(11)进程控制(Process Control)- 进程快照,用户标识符,进程调度...
  11. usaconbsp;chapternbsp;2.1nbsp;castle
  12. yuv422sp to yuv422p
  13. 【调查】35 岁以下的青年科研(青椒)人员工资多少?生活过得怎么样?
  14. 蒋鑫鸿:9.7国际黄金、纸白银行情走势分析、原油操作建议
  15. opencv半透明填充不规则区域
  16. 写给理工科人看的乐理(二)十二平均律与五线谱
  17. 现代汉语句子成分分析
  18. Unity Metaverse(五)、Avatar数字人换装系统的实现方案
  19. 运行shell脚本时报错“[[ : not found“解决方法
  20. iphone php环境,苹果(Mac OS X 10.5.6)下搭建php开发环境

热门文章

  1. vray for 3dmax2019中文版
  2. swift 3d v6.0汉化中文版
  3. 【坑爹微信】微信JSSDK图片上传问题和解决
  4. CentOS 7安装Redis服务
  5. js取整、四舍五入等数学函数
  6. sqlite不存在记录则插入数据
  7. CodeForces - 707C
  8. 安全与隐私没有允许任何来源选项
  9. 建立二叉树A【openjudge】
  10. Javascript教程:AngularJS的五个超酷特性