J - Keywords Search(AC自动机)

这篇博客探讨了如何使用AC自动机实现关键词搜索功能,特别是在图像检索系统中的应用。用户输入关键词后,系统匹配图像描述并显示匹配度最高的图像。问题简化为计算给定描述中匹配的关键词数量。通过构建字典树和使用fail指针进行失配边处理,可以在一次遍历文本串的过程中找出出现的单词个数。
摘要由CSDN通过智能技术生成

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
  • 题意分析 :

判断所给文本串中出现过的单词个数。

  • 解题思路:

首先将所给的单词建立成一个字典树,在建立过程中记录单词节点,然后用一个fail指针,来找失配边,这样指需要跑一遍文本串,就能直接得出所出现的单词个数了。

链表法 :

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>

using namespace std;

char txt[1000010];

struct Trie
{
	int v;//记录单词节点出现的次数
	Trie* next[26];
	Trie* fail;//fail指针用来找失配边
	Trie()
	{
		v = 0;
		fail = 0;
		memset(next,0,sizeof(next));//每次更新节点下面的26个节点,使其变为0
	}
};

Trie* root;

void get_trie(char *s)
{
	int i,j,c;
	int len = strlen(s);
	Trie* u = root;
	for(i = 0;i<len;i ++)
	{
		c = s[i] - 'a';
		
		if(u->next[c] == NULL)
		{
			u->next[c] = new Trie;//如果所对应的节点为空为其申请一个新的节点
		}
		
		u = u->next[c];//往下走
	}
	u->v ++;//记录单词节点出现的次数
}

void get_fail()
{
	queue<Trie*> q;
	int i,j;
	q.push(root);//根节点入队
	
	while(!q.empty())
	{
		Trie* u = q.front();//取队首元素使指针u指向它
		q.pop();//队首节点出队
		for(i = 0;i<26;i ++)
		{
			if(u->next[i] != NULL)//判断子节点是否为空
			{
				if(u == root)//判断是否为根节点,如果是将根节点对应的fail指针指向根节点
				u->next[i]->fail = root;
				else
				{
					Trie* tmp = u->fail;//创建一个中间指针,指向父亲的fail指针
					while(tmp!=NULL)//判断父亲的fail指针是否为空
					{
						if(tmp->next[i] != NULL)//父亲fail指针指向的节点的对应子节点是否为空,如果不为空将所判断的节点的fail指针指向对应的字符
						{
							u->next[i]->fail=tmp->next[i];
							break;
						}
						tmp=tmp->fail;
					}
					if(tmp == NULL)//如果父节点的fail指向空,将其更新为root
					u->next[i]->fail = root;
				}
				q.push(u->next[i]);//子节点入队
			}
		}
	}
}

int query(char *t)
{
	int sum = 0,len = strlen(t);
	Trie* u = root;
	int c,i;
	
	for(i = 0;i<len;i ++)
	{
		c = t[i] - 'a';
		
		while(u != root&&u->next[c] == NULL)//判断是否为根节点且子节点是否为空
		{
			u = u->fail;
		}
		
		u=u->next[c];//通过以上循环之后,剩下的要么是根节点,要么是子节点不为空,往下走
		if(u == NULL)//如果子节点为空,将其更新为root
		u = root;
		
		Trie* tmp = u;
		
		while(tmp != root)
		{
			if(tmp->v>0)//如果对应的单词节点不为零则将其累加到sum里面
			{
				sum += tmp->v;
				tmp->v = -1;//加过之后,防止再加
			}
			else
			break;
			
			tmp = tmp->fail;//如果找不到一直往上找相同的对应的节点
		}
	}
	return sum;
}

int main()
{
	int T,n,i,j;
	char word[10010];
	
	scanf("%d",&T);
	while(T --)
	{
		root = new Trie;
		scanf("%d",&n);
		
		for(i = 0;i<n;i ++)
		{
			scanf("%s",word);
			get_trie(word);
		}
		get_fail();
		scanf("%s",txt);
		printf("%d\n",query(txt));
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值