HDU2222-Keywords Search

本文介绍了一种使用AC自动机进行高效关键词匹配的方法。针对大量关键词搜索需求,通过构建AC自动机加速搜索过程,特别适用于图像检索系统等应用场景。文章提供了一个具体的编程实例,包括AC自动机的构造、关键词插入及查询流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Keywords Search

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


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
 

题意:输入一组字符串,最后一行输入一个字符串,问前面的字符串在最后一行的字符串中出现了几个

解题思路:ac自动机


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

char ch[1000010];

struct Trie
{
	int next[500010][26], fail[500010], cnt[500010];
	int root, tot;
	int newnode()
	{
		for (int i = 0; i < 26; i++) next[tot][i] = -1;
		cnt[tot++] = 0;
		return tot - 1;
	}
	void init()
	{
		tot = 0;
		root = newnode();
	}
	void insert(char ch[])
	{
		int k = root;
		for (int i = 0; ch[i]; i++)
		{
			if (next[k][ch[i] - 'a'] == -1) next[k][ch[i] - 'a'] = newnode();
			k = next[k][ch[i] - 'a'];
		}
		cnt[k]++;
	}
	void build()
	{
		queue<int>q;
		fail[root] = root;
		for (int i = 0; i < 26; i++)
		{
			if (next[root][i] == -1) next[root][i] = root;
			else
			{
				fail[next[root][i]] = root;
				q.push(next[root][i]);
			}
		}
		while (!q.empty())
		{
			int pre = q.front();
			q.pop();
			for (int i = 0; i < 26; i++)
			{
				if (next[pre][i] == -1) next[pre][i] = next[fail[pre]][i];
				else
				{
					fail[next[pre][i]] = next[fail[pre]][i];
					q.push(next[pre][i]);
				}
			}
		}
	}
	int query(char ch[])
	{
		int k = root, ans = 0;
		for (int i = 0; ch[i]; i++)
		{
			k = next[k][ch[i] - 'a'];
			int temp = k;
			while (temp != root)
			{
				ans += cnt[temp];
				cnt[temp] = 0;
				temp = fail[temp];
			}
		}
		return ans;
	}
	void debug()
	{
		for (int i = 0; i < tot; i++)
		{
			printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], cnt[i]);
			for (int j = 0; j < 26; j++) printf("%2d", next[i][j]);
			printf("]\n");
		}
	}
}ac;

int main()
{
	int t, n;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d", &n);
		ac.init();
		for (int i = 0; i < n; i++)
		{
			scanf("%s", ch);
			ac.insert(ch);
		}
		ac.build();
		scanf("%s", ch);
		printf("%d\n", ac.query(ch));
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值