HDU2222(Keywords Search)

Keywords Search

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

思路

AC自动机模板题(Aho-Corasick automaton),大概思路就是trie树 + bfs预处理构造fail失败指针,和kmp其实没多少关系。
在这里插入图片描述

  1. 先将所有的单词加入到一颗trie树中,然后结合层次遍历预处理这棵Trie树,根节点是空的,直接去处理第一层节点(s 和 h),它们的失败指针都指向根节点。

  2. 开始广度遍历,首先是s出队,再处理s下的所有节点(‘a’ – ‘z’)。发现编号的只有’h’,将h指向根节点的’h’(第一层的)。因为从h开始的单词有可能是带待匹配串中的一个子串。所以最后这一种情况的代码就是当前节点的失败指针就是父节点fa的失败指针指向的含有和当前节点相同字母的trie编号位

  3. 然后第二层其他节点都是同理的情况,fafail节点下没有当前字母的节点所以都指向了树根。然后第三层e节点的父亲是h,它指向了第一层h,那么同理2的加重部分,第三层的e指向第二层的e。

  4. 其他情况都一样,最后需要注意的是第三层e节点下虽然没有r节点(蓝色部分),但是对于这种虚拟没有编号的节点因为它没有编号所以不存在fail指针一说,但是可以让它的虚拟编号为9,这样就能在 这才是AC自动机的精髓。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;
const int maxn = 1e6+5;
int trie[maxn][26];
int num[maxn];
int fail[maxn];
char str[maxn];
int cnt;
void insert_s(char *s)
{
	int n = strlen(s);
	int root = 0; 
	for(int i = 0;i < n;i++){
		int k = s[i] - 'a';
		if(!trie[root][k]){
			trie[root][k] = ++cnt;
		}
		root = trie[root][k];
	}
	num[root]++;
}
void get_fail()
{
	queue<int>q;
	for(int i = 0;i < 26;i++){
		if(trie[0][i]){
			fail[trie[0][i]] = 0;
			q.push(trie[0][i]);
		}
	}
	while(!q.empty()){
		int root = q.front();
		q.pop();
		for(int i = 0;i < 26;i++){
			if(trie[root][i]){		//当前节点存在
			//失败指针指向父节点的失败指针所指向的节点下的和当前节点字母相同的节点编号值
				fail[trie[root][i]] = trie[fail[root]][i];
				q.push(trie[root][i]);
			}
			else{		//虚拟节点指向
				trie[root][i] = trie[fail[root]][i];
			}
		}
	}
	return ;
} 
void query(char *s)
{
	int n = strlen(s);
	int root = 0,ans = 0;
	for(int i = 0;i < n;i++){
		int k = s[i] - 'a';
		root = trie[root][k]; 
		for(int j = root;j && num[j];j = fail[j]){
			ans += num[j];
			num[j] = 0;
		} 
	}
	cout << endl;
	printf("%d\n",ans);
}
int main()
{
	int t,n;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		cnt = 0;
		memset(trie,0,sizeof(trie));
		memset(fail,0,sizeof(fail));
		memset(num,0,sizeof(num));
		for(int i = 0;i < n;i++){
			scanf("%s",str);
			insert_s(str); 
		}
		get_fail();
		scanf("%s",str);
		query(str);
	}
	return 0;
} 






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值