-
Notifications
You must be signed in to change notification settings - Fork 0
/
P8306 字典树.cpp
66 lines (66 loc) · 1.34 KB
/
P8306 字典树.cpp
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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
const int N = 5e5 + 5 , SZ = 3e6 + 5;
int trie[SZ][62] , cnt[SZ] , tot = 1;
bool ed[SZ];
void Insert(string s)
{
int p = 1;
for(int i = 0 ; i < s.size() ; i++)
{
int ch;
if('0' <= s[i] && s[i] <= '9')ch = s[i] - '0';
if('A' <= s[i] && s[i] <= 'Z')ch = s[i] - 'A' + 10;
if('a' <= s[i] && s[i] <= 'z')ch = s[i] - 'a' + 36;
if(trie[p][ch] == 0)trie[p][ch] = ++tot;
p = trie[p][ch];
cnt[p]++;
}
ed[p] = true;
}
int Search(string s)
{
int p = 1;
for(int i = 0 ; i < s.size() ; i++)
{
int ch;
if('0' <= s[i] && s[i] <= '9')ch = s[i] - '0';
if('A' <= s[i] && s[i] <= 'Z')ch = s[i] - 'A' + 10;
if('a' <= s[i] && s[i] <= 'z')ch = s[i] - 'a' + 36;
if(trie[p][ch] == 0)return 0;
p = trie[p][ch];
}
return cnt[p];
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int T;
cin >> T;
while(T--)
{
memset(trie , 0 , sizeof(int) * (tot + 1) * 62);
memset(cnt , 0 , sizeof(int) * (tot + 1));
memset(ed , 0 , sizeof(bool) * (tot + 1));
tot = 1;
int n , q;
cin >> n >> q;
for(int i = 1 ; i <= n ; i++)
{
string s;
cin >> s;
Insert(s);
}
for(int i = 1 ; i <= q ; i++)
{
string s;
cin >> s;
cout << Search(s) << '\n';
}
}
return 0;
}