首页 > 其他 > 详细

[HDU2222] Keywords Search

时间:2017-07-13 21:21:57      阅读:397      评论:0      收藏:0      [点我收藏+]

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

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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2896 3065 2243 2825 3341
 

思路

AC自动机
构建tire树,加上fail指针,就是一个AC自动机,然后就能AC全程了哇哈哈哈。。。
上面开玩笑的:P

代码实现

 1 #include<cstdio>
 2 #include<cstring>
 3 char s[60],m[1000010];
 4 int T,n,sz,ans;
 5 int a[500010][27],q[500010],p[500010],d[500010];
 6 bool v[500010];
 7 void ins(){
 8     int now=1,l=strlen(s);
 9     for(int i=0;i<l;i++){
10         int t=s[i]-a+1;
11         if(a[now][t]) now=a[now][t];
12         else now=a[now][t]=++sz;
13     }
14     d[now]++;
15 }
16 void acmach(){
17     int head=0,tail=1,now;
18     q[0]=1,p[1]=0;
19     while(head<tail){
20         now=q[head++];
21         for(int i=1;i<=26;i++){
22             if(!a[now][i]) continue;
23             int k=p[now];
24             while(!a[k][i]) k=p[k];
25             p[a[now][i]]=a[k][i];
26             q[tail++]=a[now][i];
27         }
28     }
29 }
30 void solve(){
31     int k=1,l=strlen(m);
32     for(int i=0;i<l;i++){
33         v[k]=1;
34         int t=m[i]-a+1;
35         while(!a[k][t]) k=p[k];
36         k=a[k][t];
37         if(!v[k]) for(int j=k;j;j=p[j]){
38             ans+=d[j];
39             d[j]=0;
40         }
41     }
42     printf("%d\n",ans);
43 }
44 int main(){
45     scanf("%d",&T);
46     while(T--){
47         sz=1,ans=0;
48         scanf("%d",&n);
49         for(int i=1;i<=26;i++) a[0][i]=1;
50         while(n--){
51             scanf("%s",s);
52             ins();
53         }
54         acmach();
55         scanf("%s",m);
56         solve();
57         for(int i=1;i<=sz;i++){
58             p[i]=d[i]=v[i]=0;
59             for(int j=1;j<=26;j++) a[i][j]=0;
60         }
61     }
62     return 0;
63 }

hzwer学长助我1A.

[HDU2222] Keywords Search

原文:http://www.cnblogs.com/J-william/p/7163104.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!