码迷,mamicode.com
首页 > 其他好文 > 详细

【HDU2222】Keywords Search(AC自动机)

时间:2016-06-14 17:37:54      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

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
 
 
 
【题意】
  给你很多个单词,然后给你一篇文章,问给出的单词在文章中出现的次数。
 
【分析】
  根据输入的串建AC自动机,不断走fail累加即可。
 
注意理解题意,同一个串出现一次只算一次,给几个比较坑的数据:
4

5
she
he
say
shr
her
yasherhs 

5
she
he
say
shr
her
yasherhs 

3
she
she
she
shesheshe

6
she
he
he
say
shr
her
yasherhs

答案:
3
3
3
4

  

代码如下:

技术分享
  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cstring>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<queue>
  7 using namespace std;
  8 #define Maxn 800100
  9 #define Maxl 60
 10 
 11 char s[Maxl];
 12 char ss[1000010];
 13 
 14 struct node
 15 {
 16     int son[30],cnt,fail;
 17 }t[Maxn];int tot;
 18 
 19 void upd(int x)
 20 {
 21     t[x].cnt=0;
 22     memset(t[x].son,0,sizeof(t[x].son));
 23 }
 24 
 25 void read_trie()
 26 {
 27     int n;
 28     scanf("%d",&n);
 29     tot=0;upd(0);
 30     for(int i=1;i<=n;i++)
 31     {
 32         int now;
 33         scanf("%s",s+1);
 34         int len=strlen(s+1);
 35         now=0;
 36         for(int j=1;j<=len;j++)
 37         {
 38             int ind=s[j]-a+1;
 39             if(!t[now].son[ind]) 
 40              t[now].son[ind]=++tot,upd(tot); 
 41             now=t[now].son[ind];
 42             if(j==len) t[now].cnt++;
 43         }
 44     }
 45 }
 46 
 47 queue<int > q;
 48 void build_AC()
 49 {
 50     int i,j,x,y;
 51     while(!q.empty()) q.pop();
 52     q.push(0);
 53     while(!q.empty())
 54     {
 55         x=q.front();
 56         y=t[x].fail;
 57         for(j=1;j<=26;j++)
 58         {
 59             if(t[x].son[j])
 60             {
 61                 q.push(t[x].son[j]);
 62                  t[t[x].son[j]].fail=x?t[y].son[j]:x;
 63             }
 64             else t[x].son[j]=t[y].son[j];
 65         }
 66         q.pop();
 67     }
 68 }
 69 
 70 int gmark(int x,int y)
 71 {
 72     if(!x) return y;
 73     if(t[x].cnt) y+=t[x].cnt,t[x].cnt=0;
 74     return gmark(t[x].fail,y);
 75 }
 76 
 77 void ffind()
 78 {
 79     scanf("%s",ss+1);
 80     int l=strlen(ss+1);
 81     int now,ans=0;
 82     for(int i=1;i<=l;i++)
 83     {
 84         int x=ss[i]-a+1;
 85         now=t[now].son[x];
 86         ans+=gmark(now,0);
 87     }
 88     printf("%d\n",ans);
 89 }
 90 
 91 int main()
 92 {
 93     int T;
 94     scanf("%d",&T);
 95     while(T--)
 96     {
 97         read_trie();
 98         build_AC();
 99         ffind();
100     }
101     return 0;
102 }
[HDU2222]

 

2016-06-14 17:02:09

 

【HDU2222】Keywords Search(AC自动机)

标签:

原文地址:http://www.cnblogs.com/Konjakmoyu/p/5584658.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!