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

hdu2222 Keywords Search【AC自动机】

时间:2018-11-02 17:54:37      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:pop   def   script   queue   mode   any   else   \n   containe   

Keywords Search

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


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
 

 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3065 2243 2825 3341 3247 
 

题意:

给定n个模式串和一个文本串,统计文本串中模式串的个数。

思路:

AC自动机模板。

多组数据一定要注意初始化!

  1 #include <iostream>
  2 #include <set>
  3 #include <cmath>
  4 #include <stdio.h>
  5 #include <cstring>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <queue>
  9 #include <map>
 10 //#include <bits/stdc++.h>
 11 using namespace std;
 12 typedef long long LL;
 13 #define inf 0x7f7f7f7f
 14 
 15 int t, n;
 16 const int maxn = 5e5 + 5;
 17 const int maxlen = 1e6 + 5;
 18 
 19 struct tree{
 20     int fail;
 21     int son[26];
 22     int ed;
 23 }AC[maxlen];
 24 int tot = 0;
 25 char s[maxlen];
 26 
 27 void build(char s[])
 28 {
 29     int len = strlen(s);
 30     int now = 0;
 31     for(int i = 0; i < len; i++){
 32         if(AC[now].son[s[i] - a] == 0){
 33             AC[now].son[s[i] - a] = ++tot;
 34         }
 35         now = AC[now].son[s[i] - a];
 36     }
 37     AC[now].ed += 1;
 38 }
 39 
 40 void get_fail()
 41 {
 42     queue<int>que;
 43     for(int i = 0; i < 26; i++){
 44         if(AC[0].son[i] != 0){
 45             AC[AC[0].son[i]].fail = 0;
 46             que.push(AC[0].son[i]);
 47         }
 48     }
 49     while(!que.empty()){
 50         int u = que.front();
 51         que.pop();
 52         for(int i = 0; i < 26; i++){
 53             if(AC[u].son[i] != 0){
 54                 AC[AC[u].son[i]].fail = AC[AC[u].fail].son[i];
 55                 que.push(AC[u].son[i]);
 56             }
 57             else{
 58                 AC[u].son[i] = AC[AC[u].fail].son[i];
 59             }
 60         }
 61     }
 62 }
 63 
 64 int AC_query(char s[])
 65 {
 66     int len = strlen(s);
 67     int now = 0, ans = 0;
 68     for(int i = 0; i < len; i++){
 69         now = AC[now].son[s[i] - a];
 70         for(int t = now; t && AC[t].ed != -1; t = AC[t].fail){
 71             ans += AC[t].ed;
 72             AC[t].ed = -1;
 73         }
 74     }
 75     return ans;
 76 }
 77 
 78 int main()
 79 {
 80     scanf("%d", &t);
 81     while(t--){
 82         for(int i = 0; i <= tot; i++){
 83             AC[i].ed = 0;
 84             AC[i].fail = 0;
 85             for(int j = 0; j < 26; j++){
 86                 AC[i].son[j] = 0;
 87             }
 88         }
 89         tot = 0;
 90         scanf("%d", &n);
 91         for(int i = 0; i < n; i++){
 92             scanf("%s", s);
 93             build(s);
 94         }
 95         AC[0].fail = 0;
 96         get_fail();
 97         scanf("%s", s);
 98         printf("%d\n", AC_query(s));
 99     }
100     return 0;
101 }

 

hdu2222 Keywords Search【AC自动机】

标签:pop   def   script   queue   mode   any   else   \n   containe   

原文地址:https://www.cnblogs.com/wyboooo/p/9897438.html

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