hdu 2222:Keywords Search(AC自动机模板)

Keywords Search

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


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
 


源代码:(203MS)

#include<iostream>
#include<string.h>
using namespace std;

const int NODEMAX=26;

struct TrieNode
{
	int count;
	struct TrieNode *fail;      //失败指针
	struct TrieNode *next[NODEMAX];//26个字母指针域
	TrieNode()
	{
		count=0;
		fail=NULL;
		memset(next,NULL,sizeof(next));
	}
};

TrieNode *q[500005];//队列


void InsertTrie(TrieNode *pRoot,char s[])//插入单词
{
	TrieNode *p=pRoot;
	if(p==NULL)
		p=pRoot=new TrieNode();
	int i=0;
	while(s[i])
	{
		int k=s[i]-'a';
		if(p->next[k]==NULL)
			p->next[k]=new TrieNode();
		i++;
		p=p->next[k];
	}
	p->count++;
}


void build_ac_automation(TrieNode *pRoot)
{
	int head=0,tail=0;
	TrieNode *p=pRoot;
	p->fail=NULL;
	q[tail++]=pRoot;
	while(head!=tail)
	{
		TrieNode *tmp=q[head++];
		for(int i=0;i<NODEMAX;i++)//设置tmp的孩子的fail指针
			if(tmp->next[i]!=NULL)
			{
				if(tmp==pRoot)//根的孩子的fail指针指向根
					tmp->next[i]->fail=pRoot;
				else
				{
					p=tmp->fail;//递增的
					while(p!=NULL)
					{
						if(p->next[i]!=NULL)
						{
							tmp->next[i]->fail=p->next[i];
							break;
						}
						p=p->fail;
					}
					if(p==NULL) tmp->next[i]->fail=pRoot;//找到,设置为根
				}
				q[tail++]=tmp->next[i]; //入队
			}
	}
}

int Search(TrieNode *pRoot,char s[])
{
	int cnt=0,k,i;
	TrieNode *p,*tmp;
	p=pRoot;
	i=0;
	while(s[i])
	{
		k=s[i]-'a';
		while(p->next[k]==NULL && p!=pRoot)//往p的失败指针回找,直至找到或返回根
			p=p->fail;
		p=p->next[k];  //一种是不通过根找到,另外或者可以通过根找到,或者为空
		if(p==NULL) p=pRoot; //空,根本找不到
		//可以往下找
		tmp=p;
		while(tmp!=pRoot && tmp->count!=-1)//以s[i]结尾的单词,通过tmp指针全部找出
		{
			cnt += tmp->count;
			tmp->count=-1;
			tmp=tmp->fail;
		}
		i++;
	}
	return cnt;
}

int main()
{
	char str[1000005],word[55];
	int T,n,i;
	cin>>T;
	while(T--)
	{
	    TrieNode *pRoot=new TrieNode();
		cin>>n;
		getchar();
		for(i=0;i<n;i++)
		{
	        gets(word);
			InsertTrie(pRoot,word);
		}
		build_ac_automation(pRoot);
		scanf("%s",str);
		cout<<Search(pRoot,str)<<endl;
	}
	system("pause");
	return 0;
}


你可能感兴趣的:(hdu 2222:Keywords Search(AC自动机模板))