-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADAINDEX.cpp
81 lines (77 loc) · 1.32 KB
/
ADAINDEX.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
int N,Q;
cin>>N>>Q;
struct Trienode
{
Trienode *children[26];
int count;
};
Trienode start;
for (int i = 0; i < 26; ++i)
{
start.children[i] = NULL;
}
start.count = 1;
for (int i = 0; i < N; ++i)
{
string str;
cin>>str;
Trienode *prev;
for (int j = 0; j < str.length(); ++j)
{
if(j == 0)
prev = &start;
if(prev->children[(int)str[j] - (int)'a'] != NULL)
{
prev = prev->children[(int)str[j] - (int)'a'];
prev->count += 1;
}
else
{
Trienode *node = new Trienode;
for (int k = 0; k < 26; ++k)
{
node->children[k] = NULL;
}
node->count = 1;
prev->children[(int)str[j] - (int)'a'] = node;
//prev->count += 1;
prev = node;
}
}
}
for (int i = 0; i < Q; ++i)
{
string str;
cin>>str;
Trienode *itr = &start;
int flag = 0;
int ans = 0;
if(str.length() == 0)
{
cout<<0<<"\n";
continue;
}
for (int j = 0; j < str.length(); ++j)
{
if(itr->children[(int)str[j] - (int)'a'] == NULL)
{
ans = 0;
//cout<<"=============================I WAS HERE===============================\n";
break;
}
else
{
itr = itr->children[(int)str[j] - (int)'a'];
ans = itr->count;
}
}
cout<<ans<<"\n";
}
return 0;
}