HDOJ 题目2222 Keywords Search(AC自动机)

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 38611    Accepted Submission(s): 12437

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   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341

ac代码

#include<stdio.h>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
char str[1001000],key[100];
int head,tail;
struct node
{
	node *fail;
	node *next[26];
	int cnt;
	node()
	{
		fail=NULL;
		cnt=0;
		for(int i=0;i<26;i++)
			next[i]=NULL;
	}
}*q[500050];
node *root;
void insert(char *s)
{
	int temp,len,i;
	node *p=root;
	len=strlen(s);
	for(i=0;i<len;i++)
	{
		temp=s[i]-'a';
		if(p->next[temp]==NULL)
			p->next[temp]=new node();
		p=p->next[temp];
	}
	p->cnt++;
}
void build_ac()
{
	q[tail++]=root;
	while(head!=tail)
	{
		node *p=q[head++];
		node *temp=NULL;
		for(int i=0;i<26;i++)
		{
			if(p->next[i]!=NULL)
			{
				if(p==root)
					p->next[i]->fail=root;
				else
				{
					temp=p->fail;
					while(temp!=NULL)
					{
						if(temp->next[i]!=NULL)
						{
							p->next[i]->fail=temp->next[i];
							break;
						}
						temp=temp->fail;
					}
					if(temp==NULL)
					{
						p->next[i]->fail=root;
					}
				}
				q[tail++]=p->next[i];
			}
		}
	}
}
int query()
{
	int ans=0;
	int len=strlen(str);
	node *p=root,*temp;
	for(int i=0;i<len;i++)
	{
		int x=str[i]-'a';
		while(p->next[x]==NULL&&p!=root)
			p=p->fail;
		p=p->next[x];
		if(p==NULL)
		{
			p=root;
		}
		temp=p;
		while(temp!=root&&temp->cnt!=-1)
		{
			ans+=temp->cnt;
			temp->cnt=-1;
			temp=temp->fail;
		}
	}
	return ans;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,i;
		scanf("%d",&n);
		head=tail=0;
		root=new node();
		for(i=0;i<n;i++)
		{
			scanf("%s",key);
			insert(key);
		}
		build_ac();
		scanf("%s",str);
		printf("%d\n",query());
	}
}
时间: 2024-08-07 17:00:35

HDOJ 题目2222 Keywords Search(AC自动机)的相关文章

HDU 2222 Keywords Search AC自动机入门题

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

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 ***********************************************************

hdoj 2222 Keywords Search(AC自动机)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包含的关键字: 假设待查找的字符串为str[0..n],则str[i…j]可能为某一个关键字:假设当前正在匹配字符str[k],则以str[i..k]为关键字的所有可能 可能的关键字的最后一个字符为str[k],使用fail指针进行跳转并判断以str[k]结尾的该结点是否为关键字最后一个结点,重复进行

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

AC自动机入门 Aho-Corasick automaton,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之一.学习AC自动机之前得先有Trie树和KMP模式匹配算法的基础. AC自动机算法分为3步:1.构造一棵tire树  2.构造失败指针  3.进行模式匹配 AC自动机的优化:Trie图 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other

HDU 2222 Keywords Search AC自动机

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

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 ac自动机入门

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串.其中模式串可以重复.问有多少文本串在模式串中出现过.(对于相同的模式串次数仍然累加) 思路:ac自动机裸题: KMP是先将文本串进行匹配得到失配边f[];但是并不适用于文本串较长,模式串较多的情况.因为每次查询的时间复杂度为O(n+m).n,m分别为文本串和模式串的长度: ac自动机就是建立在Trie上,

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

原题大意:原题链接 先给定T个单词,然后给定一个字符串,查询该字符串中包含多少个给定的单词 解题思路:AC自动机模板题 参考链接:哔哩哔哩算法讲堂 WA版本 注意:因为输入的单词可能有重复,那么Insert()函数中p->id=id;语句中p->id会被覆盖,在Query()函数中会一次性全部被清零,导致不能查询重复单词,以至于结果res错误. #include<queue> #include<cstdio> #include<cstring> using

HDU 2222 Keywords Search AC自动机模板

题目链接: hdu2222 代码: #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<queue> using namespace std; struct node { int sum; node* fail; node* next[26]; node() { sum=0; fail=NULL; for(int i=0; i<26;