Keywords Search

本文介绍了一种使用AC自动机实现的图像检索系统,通过构建关键词的自动机结构,高效地匹配文本描述中的关键词,实现了对图像描述的有效搜索。
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


题解:第一道AC自动机啊!!!确实很难理解。不过只要你肯花时间去想就会一点一点的弄懂的。注意,百度的有的代码在查询时得到的结果不对,因为考虑不全。AC自动机就是解决查询给定的多个字符串在文本字符串中出现的个数等问题的。fail指针指向的是和当前字符以及当前字符的前面的连续字符能够重合的其他串,例如:she,he。当我遍历到she中的h时,由于h的fail指向he的h,所以我会去判断he中h是不是字符串,显然不是。当到了she中e时,e的fail指向he中e,此时e是一个串。为什么这样能够找到呢??因为he和she中e和前面连续字符构成串的一样。当文本串出现she肯定就会有he。当然,我只是大概说了一点,这个是说不清楚,因为这种东西需要自己一步一步去理解。理解了就去找该算法的专题来练习。

注意:动态字典树会超时。节点太多。。用静态字典树。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

struct Node
{
	int count;  //判断字符串标记和是否找到了
	Node* fail; //指向和自己后缀匹配的其他字符串
	Node* next[26];
	Node()
	{
		count = 0;
		fail = NULL;
		for(int i = 0;i < 26;i++)
		{
			next[i] = NULL;
		}
	 } 
};

char s[1200000];
Node arr[500005];
Node* root;
int k;

void insert(char* s)
{
	int len = strlen(s);
	Node* p = root;
	for(int i = 0;i < len;i++)
	{
		int x = s[i] - 'a';
		if(p->next[x] == NULL)
		{
			p->next[x] = &arr[k++];
		}
		p = p->next[x];
	}
	
	p->count++;
}

void getFail()
{
	queue<Node*> q;
	q.push(root);
	while(!q.empty())
	{
		Node* p = q.front();
		q.pop();
		for(int i = 0;i < 26;i++)
		{
			if(p->next[i] == NULL)
			{
				continue;
			}
			q.push(p->next[i]);
			Node* t = p->fail;
			while(t)
			{
				if(t->next[i] != NULL)
				{
					p->next[i]->fail = t->next[i];
					break;
				}
				t = t->fail;
			}
			if(t == NULL)
			{
				p->next[i]->fail = root;
			}
		}
	}
}

int acSearch(char* s)
{
	Node* p = root;
	int len = strlen(s);
	int res = 0;
	for(int i = 0;i < len;i++)
	{
		int x = s[i] - 'a';
		while(p != NULL && p->next[x] == NULL)
		{
			p = p->fail;
		}
		if(p == NULL)
		{
			p = root;
			continue;
		}
		Node* t = p->next[x];
		p = p->next[x];
		while(t != NULL && t->count != -1)
		{
			if(t->count != 0)
			{
				res += t->count;
				t->count = -1;
			}
			t = t->fail;
		}
	}
	
	return res;
}

void del(Node*& root)
{
	for(int i = 0;i < 26;i++)
	{
		if(root->next[i] != NULL)
		{
			del(root->next[i]);
		}
	}
	
	delete root;
 } 

int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		root = &arr[0];
		int m;
		k = 1;
		scanf("%d",&m);
		for(int i = 0;i < m;i++)
		{
			scanf("%s",s);
			insert(s);
		}
		getFail();
		scanf("%s",s);
		printf("%d\n",acSearch(s));
		
		for(int i = 0;i < k;i++)
		{
			arr[i].count = 0;
			arr[i].fail = NULL;
			for(int j = 0;j < 26;j++)
			{
				arr[i].next[j] = NULL;
			}
		}
		//del(root);
	}
	
	return 0;
 } 


### 语义检索(Semantic Retrieve)、关键词搜索(Keywords Search)与 FAQ 查询(FAQ Query)的概念差异 语义检索是一种基于向量空间模型的检索方式,通过将查询和文档映射为高维空间中的向量,计算其语义相似度来实现匹配。这种方式能够理解自然语言的上下文含义,而不仅仅依赖于词汇的字面匹配。例如在 HyDE 方法中,系统会生成一个假设性答案并将其嵌入向量空间中进行匹配,从而提升检索精度[^1]。 关键词搜索是一种基于字面匹配的传统检索方式,它依赖于查询词与文档中关键词的重合度。这种方式通常使用倒排索引结构进行高效匹配,适用于结构化或半结构化数据的检索。关键词搜索在电子商务客服系统中被广泛使用,用于快速定位产品手册和 FAQ 数据库中的相关内容[^2]。 FAQ 查询是一种基于预定义问答对的检索机制,通常用于智能客服系统中。系统会维护一个结构化的问答知识库,当用户提出问题时,直接匹配知识库中的标准问题并返回对应的答案。这种方式响应速度快、准确率高,但灵活性较差,难以应对复杂或变体问题。 ### AI 检索中并行处理的原因 在实际的 AI 检索系统中,为了提升检索的全面性和响应速度,通常会对语义检索、关键词搜索和 FAQ 查询进行并行处理。并行处理可以同时利用不同检索方式的优势,从而提升整体检索质量。例如,在智能客服系统中,系统可以同时执行以下操作: - 使用语义检索理解用户问题的深层含义,并从产品手册和历史服务记录中提取相关答案; - 使用关键词搜索快速定位文档中与查询词匹配的内容; - 使用 FAQ 查询直接匹配知识库中的标准问题和答案。 通过并行处理,系统能够在保证响应速度的同时提高答案的准确性和多样性,从而提升用户体验。此外,并行处理还能够增强系统的容错能力,即使某一类检索方式未能找到匹配答案,其他方式仍有机会提供有效信息。 以下是一个并行检索的简化示例代码: ```python import threading def semantic_retrieve(query): # 假设该函数返回语义匹配的结果 return "Semantic result for: " + query def keyword_search(query): # 假设该函数返回关键词匹配的结果 return "Keyword result for: " + query def faq_query_match(query): # 假设该函数返回FAQ匹配的结果 return "FAQ result for: " + query def parallel_retrieval(query): results = {} def run_semantic(): results['semantic'] = semantic_retrieve(query) def run_keyword(): results['keyword'] = keyword_search(query) def run_faq(): results['faq'] = faq_query_match(query) threads = [ threading.Thread(target=run_semantic), threading.Thread(target=run_keyword), threading.Thread(target=run_faq) ] for t in threads: t.start() for t in threads: t.join() return results # 示例调用 query = "如何退货?" retrieval_results = parallel_retrieval(query) print(retrieval_results) ``` 该代码通过多线程方式并行执行三种检索方式,并将结果整合返回。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值