Sly_461的博客

The journey begins!

AC自动机(1)--hdu2222(基本模板)

                                                                                                             

                                                   Keywords Search

                                                                 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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

Hint

        这道题题意:给出n个串,然后给一篇文章,问这n个串有多少个在文章里面出现过。就是一个典型的AC自动机,

不了解的可以看我上一篇博客,值得注意的是n个串可能有相同的,需按照不同串处理。所以重复的串需要记下出现的次数,模板代码如下:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<malloc.h>
#include<queue>
using namespace std;
char str[1000000];
struct node{         
	int cnt;    //记录以该字符结尾的相同字符串有几个
	node *next[30];
	node *fail;
	void init()  
    {  
        int i;  
        for(i=0;i<26;i++)  
            next[i]=NULL;  
        cnt=0;  
        fail=NULL;  
    } 
};
node *root;
void insert(){      //建字典树
	node *p=root;         
	for(int i=0;i<strlen(str);i++){
		int pos=str[i]-'a';
		if(p->next[pos]==NULL){
			p->next[pos]=new node;
			p->next[pos]->init();
			p=p->next[pos];
		}
		else p=p->next[pos];
	}
	p->cnt++;
}
void GetFail(){     //得到每个节点的失败指针
	int i;
	node *temp;
	queue<node *>q;
	q.push(root);
	while(!q.empty()){
		temp=q.front();
		q.pop();
		for(i=0;i<26;i++){
			if(!temp->next[i])continue;
			q.push(temp->next[i]);
			if(temp==root)temp->next[i]->fail=root;
			node *p=temp->fail;
			while(p!=NULL){
				if(p->next[i]!=NULL){
					temp->next[i]->fail=p->next[i];
					break;
				}
				else p=p->fail;
				if(!p) temp->next[i]->fail=root;
			}
		}
	}
}
int Query(){       //匹配
	int i;
	int ans=0;
	int len=strlen(str);
	node *p=root;
	for(i=0;i<len;i++){
		int index=str[i]-'a';
		while(!p->next[index]&&p!=root){
			p=p->fail;
		}
		if(p->next[index]==NULL)continue;
		p=p->next[index];
		node *temp=p;
		while(temp->cnt!=0&&temp!=root){
			ans+=temp->cnt;
			temp->cnt=0;
			temp=temp->fail;
		}
	}
	return ans;
}
int main()
{
	int T,n,i;
	scanf("%d",&T);
	while(T--){
		root=new node;
		root->init();
		scanf("%d",&n);
		for(i=0;i<n;i++){
			scanf("%s",str);
			insert();
		}
		GetFail();
		scanf("%s",str);
		printf("%d\n",Query());
	}
	return 0;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Acmer_Sly/article/details/52348928
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

AC自动机(1)--hdu2222(基本模板)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭