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

题解:第一道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;
 } 

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-09 21:51:42

Keywords Search的相关文章

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(瞎搞)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 50451    Accepted Submission(s): 16236 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: 65536/32768 K (Java/Others) Total Submission(s): 42138    Accepted Submission(s): 13289 Problem Description In the modern time, Search engine came into the life of everybody like

hdu----(2222)Keywords Search(trie树)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 35683    Accepted Submission(s): 11520 Problem Description In the modern time, Search engine came into the life of everybody like

Keywords Search (ac 自动机)

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 typ

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

试个模板- - /* HDU 2222 - Keywords Search [ AC自动机 ] */ #include <bits/stdc++.h> using namespace std; const int N = 500005; const int SIZE = 26; struct Trie { int ch[N][SIZE]; int f[N], last[N], cnt[N], val[N]; int tot, ans; void init() { tot = 0; memset

AC自动机 HDU2222 Keywords Search

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

hdu2222 Keywords Search

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

hdu2222 Keywords Search ac自动机

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 56558    Accepted Submission(s): 18493 Problem Description In the mo