hdu 2222 Keywords Search

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    

Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 33431    Accepted Submission(s): 10800


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
 
解题:AC自动机,多模式匹配问题,我还是不怎么明白有些问题的细节是怎么处理的。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #define LL long long
13 #define INF 0x3f3f3f3f
14 using namespace std;
15 const int maxn = 250010;
16 struct trie {
17     int letter[26],fail;
18     int cnt;
19 } dic[maxn];
20 int tot = 1;
21 char str[1000100];
22 void insertWord(int root,char *s) {
23     for(int i = 0; s[i]; i++) {
24         int k = s[i]-'a';
25         if(dic[root].letter[k] == -1)
26             dic[root].letter[k] = tot++;
27         root = dic[root].letter[k];
28     }
29     dic[root].cnt++;
30 }
31 queue<int>q;
32 void build(int root) {
33     dic[root].fail = root;
34     q.push(root);
35     while(!q.empty()) {
36         int u = q.front(),v;
37         q.pop();
38         for(int i = 0; i < 26; i++) {
39             if(dic[u].letter[i] == -1) continue;
40             if(u == 0) dic[dic[u].letter[i]].fail = 0;
41             else {
42                 v = dic[u].fail;
43                 while(v && dic[v].letter[i] == -1) v = dic[v].fail;
44                 if(dic[v].letter[i] == -1) dic[dic[u].letter[i]].fail = 0;
45                 else dic[dic[u].letter[i]].fail = dic[v].letter[i];
46             }
47             q.push(dic[u].letter[i]);
48         }
49     }
50 }
51 int query(int root,char *s) {
52     int i,ans = 0;
53     for(i = 0; s[i]; i++) {
54         int k = s[i]-'a';
55         while(root && dic[root].letter[k] == -1)
56             root = dic[root].fail;
57         if(dic[root].letter[k] != -1) {
58             int v = dic[root].letter[k];
59             while(dic[v].cnt) {
60                 if(dic[v].cnt) {
61                     ans += dic[v].cnt;
62                     dic[v].cnt = 0;
63                 }
64                 v = dic[v].fail;
65             }
66             ans += dic[v].cnt;
67             dic[v].cnt = 0;
68             root = dic[root].letter[k];
69         }
70     }
71     return ans;
72 }
73 int main() {
74     int i,j,m,t;
75     char temp[100];
76     scanf("%d",&t);
77     while(t--) {
78         tot = 1;
79         scanf("%d",&m);
80         while(!q.empty()) q.pop();
81         for(i = 0; i < maxn; i++) {
82             dic[i].cnt = 0;
83             dic[i].fail = 0;
84             memset(dic[i].letter,-1,sizeof(dic[i].letter));
85         }
86         while(m--) {
87             scanf("%s",temp);
88             insertWord(0,temp);
89         }
90         build(0);
91         scanf("%s",str);
92         printf("%d
",query(0,str));
93     }
94     return 0;
95 }
View Code
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1000010;
 4 struct Trie {
 5     int ch[maxn][26],fail[maxn],cnt[maxn],tot;
 6     int newnode() {
 7         memset(ch[tot],0,sizeof ch[tot]);
 8         fail[tot] = cnt[tot] = 0;
 9         return tot++;
10     }
11     void init() {
12         tot = 0;
13         newnode();
14     }
15     void insert(char *str,int root = 0) {
16         for(int i = 0; str[i]; ++i) {
17             if(!ch[root][str[i]-'a'])
18                 ch[root][str[i]-'a'] = newnode();
19             root = ch[root][str[i]-'a'];
20         }
21         ++cnt[root];
22     }
23     void build(int root = 0) {
24         queue<int>q;
25         for(int i = 0; i < 26; ++i)
26             if(ch[root][i]) q.push(ch[root][i]);
27         while(!q.empty()) {
28             root = q.front();
29             q.pop();
30             for(int i = 0; i < 26; ++i) {
31                 if(!ch[root][i]) ch[root][i] = ch[fail[root]][i];
32                 else {
33                     fail[ch[root][i]] = ch[fail[root]][i];
34                     q.push(ch[root][i]);
35                 }
36             }
37         }
38     }
39     int query(char *str,int root = 0,int ret = 0) {
40         for(int i = 0; str[i]; ++i){
41             int x = root = ch[root][str[i]-'a'];
42             while(x && cnt[x]){
43                 ret += cnt[x];
44                 cnt[x] = 0;
45                 x = fail[x];
46             }
47         }
48         return ret;
49     }
50 }ac;
51 char str[maxn*10];
52 int main() {
53     int kase,n;
54     scanf("%d",&kase);
55     while(kase--){
56         scanf("%d",&n);
57         ac.init();
58         while(n--){
59             scanf("%s",str);
60             ac.insert(str);
61         }
62         ac.build();
63         scanf("%s",str);
64         printf("%d
",ac.query(str));
65     }
66     return 0;
67 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/3873017.html