Keywords Search AC自动机
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
AC自动机:利用ttie树节省空间,利用kmp相似的原理构建fail指针进行匹配
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 26
#define L 31
#define INF 1000000009
#define eps 0.00000001
struct node
{
node()
{
cnt = ;
fail = NULL;
for (int i = ; i < MAXN; i++)
next[i] = NULL;
}
int cnt;
struct node* next[MAXN];
struct node* fail;
};
typedef struct node* Tree;
void insert(Tree T, const char* str)
{
int p = ;
Tree tmp = T;
while (str[p] != '\0')
{
int k = str[p] - 'a';
if (tmp->next[k] == NULL)
{
tmp->next[k] = new node();
}
tmp = tmp->next[k];
p++;
}
tmp->cnt++;
}
void build_ac(Tree root)
{
root->fail = NULL;
queue<Tree> q;
q.push(root);
while (!q.empty())
{
Tree tmp = q.front();
q.pop();
Tree p = NULL;
for (int i = ; i < MAXN; i++)
{
if (tmp->next[i] != NULL)
{
if (tmp == root)
tmp->next[i]->fail = root;
else
{
p = tmp->fail;
while (p != NULL)
{
if (p->next[i] != NULL)
{
tmp->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if (p == NULL) tmp->next[i]->fail = root;
}
q.push(tmp->next[i]);
}
}
}
}
int query(Tree root,const char* str)
{
int i = ;
int count = ;
int l = strlen(str);
Tree tmp = root;
int index;
while (str[i])
{
index = str[i] - 'a';
while (tmp->next[index] == NULL && tmp != root)
tmp = tmp->fail;
tmp = tmp->next[index];
if (tmp == NULL) tmp = root;
Tree p = tmp;
while (p != root)
{
count += p->cnt;
p->cnt = ;
p = p->fail;
}
i++;
}
return count;
} void del(Tree root)
{
for (int i = ; i < MAXN; i++)
if (root->next[i] != NULL)
del(root->next[i]);
delete root;
}
char key[], s[];
int main()
{
int T, n;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
Tree root = new node();
root->cnt = ;
root->fail = NULL;
for (int i = ; i < MAXN; i++)
root->next[i] = NULL;
for (int i = ; i < n; i++)
{
scanf("%s", key);
insert(root, key);
}
build_ac(root);
scanf("%s", s);
printf("%d\n", query(root, s));
del(root);
}
}
Keywords Search AC自动机的更多相关文章
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
随机推荐
- 例题 3-5 谜题 uva227 Puzzle
A children’s puzzle that was popular years ago consisted of a × frame which contained small squares ...
- python2 'str' object has no attribute 'decode'
'.decode('hex') 上述代码,报错: 'str' object has no attribute 'decode' 查找原因: https://stackoverflow.com/ques ...
- C# 堆VS栈 值类型VS引用类型
最近博客园上连续出现了几篇关于堆VS栈 值类型VS引用类型的文章. 一个是关于C# 堆VS栈的,深入浅出,动图清晰明了,链接如下 C#堆栈对比(Part One) C#堆栈对比(Part Two) C ...
- 几个不同的tab切换示例
上一篇<论tab切换的几种实现方法>中讲了tab切换的4种不同实现原理,那么,现在到理论联系实际的时候了,下面就写几个实例. 一.仿”中国人民大学“官网的tab切换,背景是图片,效果图如下 ...
- redis 配置多个ip 解决方案
因为在 redis 中bind 指定的ip 其实为同一网段或localhost 监听ip,在这里配置 内网其他网段或者外网多个ip 后 重启 redis 是不会成功的, 这边建议使用 折中方案,开通 ...
- opencv函数之cv.InRange函数
2018-03-0421:22:46 (1)cv.InRange函数 void cvInRange(//提取图像中在阈值中间的部分 const CvArr* src,//目标图像const CvArr ...
- Selenium示例集锦--常见元素识别方法、下拉框、文本域及富文本框、鼠标操作、一组元素定位、弹窗、多窗口处理、JS、frame、文件上传和下载
元素定位及其他操作 0.常见的识别元素的方法是什么? driver.find_element_by_id() driver.find_element_by_name() driver.find_ele ...
- 读《An Adaptable and Extensible Geometry Kernel》
读<An Adaptable and Extensible Geometry Kernel> 利用Curiously Recurring Template Pattern替代虚函数 详细内 ...
- 【Python-2.7】换行符和制表符
在Python中换行符“\n”表示接下来的内容将会换到下一行显示,制表符“\t”表示下面的内容显示时在前面留出空白,如打印如下内容: Dear: I love you forever! 上面的一段话分 ...
- HDU多校Round 5
Solved:3 rank:71 E. Everything Has Changed #include <bits/stdc++.h> using namespace std; const ...