http://acm.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): 67751    Accepted Submission(s): 22807



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自动机,多模字符串匹配。

前导课程 KMP单模匹配算法、字典树。

推荐讲解:点击打开链接

这里是一道模板题

【代码】:


#include <stdio.h>
#include <stdlib.h>  
#include <string.h>  
#include <iostream>  
#include <algorithm> 
#include <queue>
using namespace std;
const int MAX=1e6+5;
const int M=26;
struct dictree{
	char ch;//此处的字母
	int sum;//单词末尾,以及这个单词的次数 
	dictree *fail;//失配指针
	dictree *next[M];//子节点
	dictree(){
		ch=0;
		sum=0;
		fail=NULL;
		memset(next,NULL,sizeof(next));
	}
}*root;
int insert(dictree *root,char s[])//增加一个新单词
{
	dictree *p=root;
	for(int i=0;s[i];i++)
	{
		int k=s[i]-'a';
		if(p->next[k]==NULL)//若无子节点,申请一个
			p->next[k]=new dictree();
		p=p->next[k];
		p->ch=s[i];
	}
	p->sum++;//结尾
}
void getfail(dictree *root)//字典树自我匹配 
{
	queue<dictree*>q;//只针队列 
	q.push(root);
	dictree *p=root,*temp;
	while(!q.empty())
	{
		p=q.front();q.pop();
		for(int i=0;i<M;i++)if(p->next[i])//子节点 
		{
			if(p==root)//根的子节点fail指向根
				p->next[i]->fail=root;
			else
			{
				temp=p->fail;//temp指向此节点的失配节点
				while(temp)
				{
					if(temp->next[i])//找到失配节点
					{
						p->next[i]->fail=temp->next[i];
						break;
					}
					temp=temp->fail;//回退 
				}
				if(!temp)p->next[i]->fail=root;//失配到根 
			}
			q.push(p->next[i]);
		}
	}
}
int acfind(char s[],dictree *root)//字典树去匹配长串s 
{
	getfail(root);
	int ans=0;//匹配到的单词数量 
	dictree *p=root,*temp;
	for(int i=0;s[i];i++)
	{
		int k=s[i]-'a';
		while(p->next[k]==NULL&&p!=root)//失配回退
			p=p->fail;
		p=p->next[k];
		if(!p)p=root;//没子节点,重来
		
		temp=p;
		while(temp!=root&&temp->sum!=-1)
		{
			ans+=temp->sum;
			temp->sum=-1;
			temp=temp->fail;
		}
	}
	return ans;
} 

void FREE(dictree *p)//释放空间 
{
	if(!p){
		return;
	}
	for(int i=0;i<M;i++)
		FREE(p->next[i]);
	free(p);
}
int main()
{
	char s[MAX];
	int T,n;
	scanf("%d",&T);
	while(T--)
	{
		root=new dictree();
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		{
			scanf("%s",s);
			insert(root,s);
		}
		scanf("%s",s);
		int ans=acfind(s,root);
		cout<<ans<<endl;
		FREE(root);
	}
	return 0;
}