HDU-2222文字检索

题目:

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.

InputFirst 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. 
OutputPrint how many keywords are contained in the description.Sample Input

1
5
she
he
say
shr
her
yasherhs

Sample Output

3题意就是说给你一些模式串,问你有哪几个在目标串中出现过,(⊙o⊙)…,和洛谷那道末班体一模一样,就是多了一个多组数据。
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<cstring>
 6 #include<queue>
 7 using namespace std;
 8
 9 int n,cnt=1;
10 char s[1000007];
11 struct Node
12 {
13     int c[26],suf,flag;
14     void init()
15     {
16         suf=flag=0;
17         memset(c,0,sizeof(c));
18     }
19 }tree[1000007];
20
21 void init()
22 {
23     for (int i=0;i<=cnt;i++)
24         tree[i].init();
25     cnt=1;
26 }
27 void Ins()
28 {
29     int head=1,l=strlen(s);
30     for (int i=0;i<l;i++)
31     {
32         int now=s[i]-‘a‘;
33         if (!tree[head].c[now]) tree[head].c[now]=++cnt;
34         head=tree[head].c[now];
35     }
36     tree[head].flag++;
37 }
38 void Make_AC()
39 {
40     for (int i=0;i<26;i++) tree[0].c[i]=1;
41     int head=0,tail=1;
42     queue<int>q;
43     while (!q.empty()) q.pop();
44     tree[1].suf=0;
45     q.push(1);
46     while (!q.empty())
47     {
48         int u=q.front();
49         q.pop();
50         for (int i=0;i<26;i++)
51             if (tree[u].c[i])
52             {
53                 tree[tree[u].c[i]].suf=tree[tree[u].suf].c[i];
54                 q.push(tree[u].c[i]);
55             }
56             else tree[u].c[i]=tree[tree[u].suf].c[i];
57     }
58 }
59 void Solve()
60 {
61     scanf("%s",s);
62     int ans=0,head=1,len=strlen(s);
63     for (int i=0;i<len;i++)
64     {
65         int now=s[i]-‘a‘;
66         head=tree[head].c[now];
67         for (int j=head;j&&tree[j].flag!=-1;j=tree[j].suf)
68             ans+=tree[j].flag,tree[j].flag=-1;
69     }
70     printf("%d\n",ans);
71 }
72 int main()
73 {
74     int Cas;
75     scanf("%d",&Cas);
76     while (Cas--)
77     {
78         init();
79         scanf("%d",&n);
80         while (n--){scanf("%s",s);Ins();}
81         Make_AC();
82         Solve();
83     }
84 }
 
时间: 2024-10-12 07:53:30

HDU-2222文字检索的相关文章

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

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 67950    Accepted Submission(s): 22882 Problem Description In the modern time, Search engine came into the life of everybody lik

HDU 2222 - Keywords Search

试个模板- - /* HDU 2222 - Keywords Search [ AC自动机 ] */ #include <bits/stdc++.h> using namespace std; const int N = 500005; const int SIZE = 26; struct Trie { int ch[N][SIZE]; int f[N], last[N], cnt[N], val[N]; int tot, ans; void init() { tot = 0; memset

HDU 2222 Keyword Search AC自动机模板

#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <cmath> #include <stack> #include <map> #include <ctime> #include <io

hdu 2222 Keywords Search(ac自动机入门题)

1 /************************************************************ 2 题目: Keywords Search(hdu 2222) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 4 算法: ac自动机 5 算法思想: 多个字符串匹配,也就是相当于多个kmp 6 ***********************************************************

HDU 2222——Keywords Search(AC自动机)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 34020    Accepted Submission(s): 11009 Problem Description In the modern time, Search engine came into the life of everybody like

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

HDU 2222 题意:给出N(N<=10,000)个单词,每个单词长度不超过50.再给出一个字符串S,字符串长度不超过1,000,000.问有多少个单词出现在了字符串S中.(单词可能重复,单词在S中允许重叠) 我们在val[]数组中记录重复的个数 因为在主串对他进行匹配时,匹配到一次后这个重复次数就不应该继续添加了,所以在query中val[]要对它进行清零操作 #include <cstdio> #include <cstring> #include <queue&

HDU 2222 Keywords Search AC自动机入门题

单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自动机的基础: 1 Trie, 以这个数据结构为基础的,不过增加一个fail指针和构造fail的函数 2 KMP,不是直接运用KMP,而是需要KMP的思想,KMP思想都没有的话,理解这个算法会更加吃力的. 注意本题的单词会有重复出现的,一个单词只能统计一次. 搜索了一下网上的题解,发现好多代码都是一大抄的啊,⊙﹏⊙b汗. 本博客的乃是原创代码,代码风格也是差不多固定的,转载请注明出处:http://blog.c

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

http://acm.hdu.edu.cn/showproblem.php?pid=2222 KMP是单模式串匹配的算法,而AC自动机是用于多模式串匹配的算法.主要由Trie和KMP的思想构成. 题意:输入N个模式串,再给出一个文本串,求文本串里出现的模式串数目. 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <cstdlib> 5 #include <al

HDU 2222(AC自动机模板题)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题思路: AC自动机模板题. 一开始使用LRJ的坑爹静态模板,不支持重复的模式串. 在做AC自动机+DP的时候,扒了zcwwzdjn大神的动态优化(失配指向root)写法,以及借鉴了网上的AC自动机模板, 搞出了这么一个支持重复串的模板. #include "cstdio" #include

HDU 2222 Keywords Search[自动机]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:给n个模板串,求长串中模板串的个数: 解题思路: 自动机入门题,注意模板重复串. 代码: #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include&l