题目描述
题目链接
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.
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.
1
2
3
4
5
6
7
8
| 1
5
she
he
say
shr
her
yasherhs
|
Sample Output
题解
就是一个模板题, 直接上代码, 若想看详细解释请看这篇博客
AC代码(指针版)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
| //#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define esp 1e-6
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int N = 1e6 + 5;
const int M = 1e9 + 5;
struct node{
struct node *fail; // 这个节点的fail指针
node *next[30]; // 这个节点的儿子节点
int count; // 这个节点是不是单词的末尾节点(数量)
node(){
fail = NULL;
count = 0;
memset(next, NULL, sizeof next);
}
}*q[N];
char keyword[N]; // 输入的单词
char str[N]; // 模式串
int tail, head;
void build_Trie(char *str, node *root){ // 构建trie
node *p = root;
int i = 0, index;
while(str[i]){
index = str[i] - 'a';
if (p->next[index] == NULL)
p->next[index] = new node(); // 不加括号应该也行吧
p = p->next[index];
i++;
}
p->count++;
}
void build_ac_automation(node *root){
root->fail = NULL;
q[head++] = root;
while(head != tail){
node *temp = q[tail++];
node *p = NULL;
for (int i =0; i < 26; i++){
if (temp->next[i] != NULL){
if (temp == root)
temp->next[i]->fail = root;
else{ // 父节点的fail 的子节点
p = temp->fail;
while(p != NULL){
if (p->next[i] != NULL){
temp->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if (p == NULL)
temp->next[i]->fail = root;
}
q[head++] = temp->next[i];
}
}
}
}
int query(node *root){
int i = 0, cnt = 0, index, len = strlen(str);
node *p = root;
while(str[i]){
index = str[i] - 'a';
while(p->next[index] == NULL && p != root)
p=p->fail;
p = p->next[index];
if (!p)
p = root;
node *temp = p;
while(temp != root && temp->count != -1){
cnt += temp->count;
temp->count = -1;
temp = temp->fail;
}
i++;
}
return cnt;
}
int main(){
int t, n;
scanf("%d", &t);
while(t--){
head = tail = 0;
scanf("%d", &n);
node *root = new node;
while(n--){
scanf("%s", keyword);
build_Trie(keyword, root);
}
scanf("%s", str);
build_ac_automation(root);
printf("%d\n", query(root));
}
return 0;
}
|
1
| 恰似你一低头的温柔,娇弱水莲花不胜寒风的娇羞, 我的心为你悸动不休。 --mingfuyan
|