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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值