您的位置:首页 > 其它

HDU 2222 Keywords Search

2013-06-18 15:33 399 查看
题目链接 : HDU2222

Keywords Search

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

Total Submission(s): 25120    Accepted Submission(s): 8221


[align=left]Problem Description[/align]
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.

 

 

[align=left]Input[/align]
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.

 

 

[align=left]Output[/align]
Print how many keywords are contained in the description.
 

 

[align=left]Sample Input[/align]

1
5
she
he
say
shr
her
yasherhs

 

 

[align=left]Sample Output[/align]

3

 

 

[align=left]Author[/align]
Wiskey
 

 

[align=left]Recommend[/align]
lcy
 
好吉利的题号  ╮( ̄▽ ̄)╭

AC自动机模板题

 

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
using namespace std;

int to[260005][26],fail[500005],cont[500005];
int que[500005],k;
char str[1000005],lib[55];
void add_trie(char a[])
{
int index,p=0;
for(int i=0;a[i];i++)
{
index=a[i]-'a';
if(to[p][index]==0)
{
memset(to[k],0,sizeof(to[k]));
fail[k]=cont[k]=0;
to[p][index]=k++;
}
p=to[p][index];
}
cont[p]++;
}
void build_ac()
{
int tail=0,front=0,p;
for(int i=0;i<26;i++)
if(to[0][i])
{
fail[to[0][i]]=0;
que[front++]=to[0][i];
}
while(tail<front)
{
p=que[tail++];
for(int i=0;i<26;i++)
if(to[p][i])
{
fail[to[p][i]]=to[fail[p]][i];
que[front++]=to[p][i];
}
else to[p][i]=to[fail[p]][i];
}
}
int query(char a[])
{
int p=0,ans=0,temp;
for(int i=0;a[i];i++)
{
p=to[p][a[i]-'a'];
temp=p;
while(temp && cont[temp]!=-1)
{
ans+=cont[temp];
cont[temp]=-1;
temp=fail[temp];
}
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
memset(to[0],0,sizeof(to[0]));
cont[0]=0;k=1;
for(int i=0;i<n;i++)
{
scanf("%s",lib);
add_trie(lib);
}
build_ac();
scanf("%s",str);
printf("%d\n",query(str));
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: