hdu 2222 keywords search

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 48475    Accepted Submission(s): 15457


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
 
题目大意:给出n个单词,再给出一个文本,求文本中出现过的单词个数。有多组数据

题解:这道题是ac自动机的模板题,调了很久才过,有些小问题需要注意

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
using namespace std;
int len,n,i,m;
char s[1000003];
namespace ac
{
	int ch[500003][26],fail[500003],cnt[500003],tot=0;
	int is_end[500003],visited[500003];
	inline void clear()
	{
		memset(ch,0,sizeof(ch)); 
		memset(fail,0,sizeof(fail));
		memset(is_end,0,sizeof(is_end));
		memset(visited,0,sizeof(visited));
		tot=0;
	}
	inline void insert(char *s)//把单词加入trie树
	 {
	 	int len=strlen(s); int now=0;
	 	for (int i=0;i<len;i++)
	 	 {
	 	 	int x=s[i]-'a';
	 	 	if (!ch[now][x])
	 	 	 ch[now][x]=++tot;
	 	 	now=ch[now][x];
	 	 }
	 	is_end[now]++;//这里需要注意,同一个单词不一定只会给出一个,有可能n个单词中有重复的,刚开始直接赋值为1,wrong ans了n次
	 }
	inline void make_fail()//构建失配指针
	 {
	   queue<int> p;
	   for (int i=0;i<26;i++)
	    if (ch[0][i]) p.push(ch[0][i]);
	   while (!p.empty())
	    {
	      int now=p.front(); p.pop();
	      for (int i=0;i<26;i++)
	       {
	       	 if (!ch[now][i])
	       	  {
	       	  	 continue;
	       	  }
	       	 int x=ch[now][i];
	       	 int k=fail[now];
	       	 while (!ch[k][i]&&k!=0)  k=fail[k];
	       	 fail[x]=ch[k][i];
	       	 p.push(x);
	       }
	    }	
	 }
	inline void solve(char *s)
	 {
	 	int ans=0;
	 	int len=strlen(s); int now=0;
	 	for (int i=0;i<len;i++)
	 	 {
	 	 	visited[now]=1;//标记数组,因为要统计的是个数而不是出现次数,所以一个单词只能被算一次,只经过一次
	 	 	int x=s[i]-'a';
	 	 	while (now!=0&&!ch[now][x])
	 	 	 now=fail[now];
	 	 	int m=ch[now][x];
	 	 	if (!visited[m])
	 	 	while (m!=0)
	 	 	{
	 	 	  ans+=is_end[m];
	 	 	  is_end[m]=0;
	 	 	  m=fail[m];
	 	 	}
	 	 	now=ch[now][x];
	 	 }
	 	printf("%d\n",ans);
	 }
}
int main()
{
  scanf("%d",&m);
  for (int j=1;j<=m;j++)
  {
  ac::clear();
  scanf("%d",&n);
  for (i=1;i<=n;i++)
   {
   	 scanf("%s",s);
	 ac::insert(s); 
   }
  ac::make_fail();
  scanf("%s",s);
  ac::solve(s);
  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值