现在的位置: 首页 > 综合 > 正文

HDU2222—Keywords Search

2019年02月15日 ⁄ 综合 ⁄ 共 3049字 ⁄ 字号 评论关闭

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38092    Accepted Submission(s): 12269

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   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341 
 

AC自动机模版题

/*************************************************************************
    > File Name: hdu2222.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年02月04日 星期三 19时25分57秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
const int N = 1000010;
char str[55];
char buf[N];

struct node
{
		node *next[26]; //根据题目而定
		node *fail;
		int count;
		node ()
		{
			fail = NULL;
			for (int i = 0; i < 26; ++i)
			{
				next[i] = NULL;
			}
			count = 0;
		}
};

node *qu[500010];

class AC_Automation
{
	private:
		node *root;
		int head;
		int tail;
	public:
		AC_Automation()
		{
			head = 0;
			tail = 0;
			root = new node();
		}

		void Build_Trie(char str[])
		{
			int len = strlen (str);
			node *p = root;
			for (int i = 0; i < len; ++i)
			{
				int ind = str[i] - 'a';
				if (p -> next[ind] == NULL)
				{
					p -> next[ind] = new node();
				}
				p = p -> next[ind];
			}
			++p -> count;
		}

		void Build_AC ()
		{
			root -> fail = NULL;
			qu[tail++] = root;
			while (head < tail)
			{
				node *now = qu[head++];
				node *p = NULL;
				for (int i = 0; i < 26; ++i)
				{
					if (now -> next[i] != NULL)
					{
						if (now == root)
						{
							now -> next[i] -> fail = root;
						}
						else
						{
							p = now -> fail;
							while (p != NULL)
							{
								if (p -> next[i] != NULL)
								{
									now -> next[i] -> fail = p -> next[i];
									break;
								}
								else
								{
									p = p -> fail;
								}
							}
						}
						if (p == NULL)
						{
							now -> next[i] -> fail = root;
						}
						qu[tail++] = now -> next[i];
					}
				}
			}
		}

		int Match (char buf[])
		{
			int ans = 0;
			int len = strlen (buf);
			node *p = root;
			for (int i = 0; i < len; ++i)
			{
				int ind = buf[i] - 'a';
				while (p -> next[ind] == NULL && p != root)
				{
					p = p -> fail;
				}
				p = p -> next[ind];
				if (p == NULL)
				{
					p = root;
				}
				node *tmp = p;
				while (tmp != root && tmp -> count != -1)
				{
					ans += tmp -> count;
					tmp -> count = -1;
					tmp = tmp -> fail;
				}
			}
			return ans;
		}
		
		void dfs (node *p)
		{
			for (int i = 0; i < 26; ++i)
			{
				if (p -> next[i] != NULL)
				{
					dfs (p -> next[i]);
				}
			}
			delete p;
		}

		~AC_Automation()
		{
			dfs (root);
		}
};

int main ()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		int n;
		scanf("%d", &n);
		AC_Automation AC;
		for (int i = 1; i <= n; ++i)
		{
			scanf("%s", str);
			AC.Build_Trie(str);
		}
		scanf("%s", buf);
		AC.Build_AC();
		int ans = AC.Match(buf);
		printf("%d\n", ans);
	}
	return 0;
}

抱歉!评论已关闭.