AC自动机 HDU2222 Keywords Search

Keywords Search

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

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

模板题,模板源自chaijing

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 int t,n,num;
 8 int len,val,j;
 9 char s[1000010];
10 struct data{
11     int ch[30];
12     int fail,cnt;//cnt 以该节点结束的词的个数
13 }tree[1000010];
14 queue<int>q;
15 void clear(int x){
16     memset(tree[x].ch,0,sizeof(tree[x].ch));
17     tree[x].fail=0;
18     tree[x].cnt=0;
19 }
20 void trie(int x){
21     len=strlen(s);
22     for(int i=0;i<len;i++){
23         val=s[i]-‘a‘+1;
24         if(!tree[x].ch[val]){
25             num++;
26             clear(num);
27             tree[x].ch[val]=num;
28         }
29         x=tree[x].ch[val];
30     }
31     tree[x].cnt++;
32 }
33 void build(){
34     while(!q.empty()) q.pop();
35     for(int i=1;i<=26;i++)
36         if(tree[0].ch[i]) q.push(tree[0].ch[i]);
37     while(!q.empty()){
38         int p=q.front();
39         q.pop();
40         for(int i=1;i<=26;i++){
41             int fail=tree[p].fail;
42             if(tree[p].ch[i]){
43
44                 tree[tree[p].ch[i]].fail=tree[fail].ch[i];
45                 q.push(tree[p].ch[i]);
46             }
47             else tree[p].ch[i]=tree[fail].ch[i];
48         }
49     }
50 }
51 int find(int x){
52     int ans=0;
53     len=strlen(s);
54     for(int i=0;i<len;i++){
55         val=s[i]-‘a‘+1;
56         while(x&&!tree[x].ch[val]) x=tree[x].fail;
57         x=tree[x].ch[val];
58         j=x;
59         while(j&&tree[j].cnt!=-1){
60             ans+=tree[j].cnt;
61             tree[j].cnt=-1;
62             j=tree[j].fail;
63         }
64     }
65     return ans;
66 }
67 int main(){
68     scanf("%d",&t);
69     while(t){
70         t--;
71         num=0;
72         clear(0);
73         scanf("%d",&n);
74         for(int i=1;i<=n;i++){
75             scanf("%s",s);
76             trie(0);
77         }
78         build();
79         scanf("%s",s);
80         printf("%d\n",find(0));
81     }
82     return 0;
83 }
时间: 2024-08-07 21:17:42

AC自动机 HDU2222 Keywords Search的相关文章

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

AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! 1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <ctime> 7 #include <cstdlib> 8 #include <queue>

hdu2222 【AC自动机】Keywords Search

题意 给定n个模式串,求目标串中出现了多少个模式串. 传送门 思路 AC自动机模版题. Code #include <bits/stdc++.h> using namespace std; const int maxn = 1e6+10; struct Ac { int tr[maxn][26], fail[maxn], e[maxn], cnt[maxn]; int tot; void init() { memset(tr, 0, sizeof(tr)); memset(e, 0, size

C++之路起航——AC自动机(Keywords Search)

/*Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 47794 Accepted Submission(s): 15228 Problem DescriptionIn the modern time, Search engine came into the life of everybody like Goo

hdu2222 Keywords Search ac自动机

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 56558    Accepted Submission(s): 18493 Problem Description In the mo

hdu2222 Keywords Search &amp; AC自动机学习小结

传送门:http://http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:AC自动机入门题,直接上AC自动机即可. 对于构建AC自动机,我们要做的只有三件事: 1)构建字典树 2)构建失败指针 3)构建trie图(这道题好像不做这一步也能A...但是这一步不做是会被卡成O(n^2)的...) 1)第一步还是比较好理解的 根是虚根,边代表字母,那么根到终止节点的路径就是一个字符串,这样对于前缀相同的字符串我们就可以省下存公共前缀的空间. 加入一个模式

HDU2222 Keywords Search(AC自动机)

AC自动机是一种多模式匹配的算法.大概过程如下: 首先所有模式串构造一棵Trie树,Trie树上的每个非根结点都代表一个从根出发到该点路径的字符串. 然后每个结点都计算出其fail指针的值,这个fail指针就指向这个结点所表示字符串的最长存在的后缀所对应的结点,如果不存在就指向根:计算每个结点的fail用BFS,比如当前结点u出队要拓展并计算其孩子结点的fail,v是其第k个孩子,fail[v]的值就是某个fail[fail[fail...[u]]]存在第k孩子结点其第k个孩子结点,如果不存在f

HDU2222 Keywords Search【AC自动机】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意: 给你N个模式串,和一个文本串.问:文本串中共出现了几个模式串. 思路: 这道题是AC自动机的基础题目.就是求文本串中出现的模式串个数.用Val[]数组来标记模式串. 最后用ans累加模式串个数. AC代码: #include<iostream> #include<algorithm> #include<cstdio> #include<cstrin

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

题目大意:统计一共出现了多少次模板串. 题目分析:AC自动机的模板题.不过这题有坑,相同的模板串不能只算一次. 代码如下: # include<iostream> # include<cstdio> # include<queue> # include<map> # include<string> # include<cstring> # include<algorithm> using namespace std; co

hdu2222 Keywords Search(AC自动机初步)

题目大意: 给出多个模式串和一个主串,求多少个模式串在主串中出现过. 传送门 这是一道AC自动机的模板题. 在学习AC自动机之前,首先要学习WA自动机.TLE自动机和MLE自动机(雾 AC自动机是一种多模式串匹配算法. AC自动机概述: *fail指针:指向失配时的匹配节点: 1)构建字典树 2)初始化fail指针: 一条$fail$指针链可以理解为一个串连所有后缀相同的字符串的链表,并且所有链表的末端都指向trie的根.我们定义沿着节点$v$的$fail$指针走到根部的路径为v的trie链表.