-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain1014.cpp
72 lines (66 loc) · 1.11 KB
/
main1014.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
//http://hihocoder.com/problemset/problem/1014 #1014 : TrieÊ÷
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct node
{
int next[26];
int cnt;
}T[1000000];
int le;
void insert(char * s)
{
int i=0 ,p=0;
while(s[i])
{
int x = s[i]-'a';
if(T[p].next[x]==-1)
{
T[le].cnt=0;
memset(T[le].next,-1,sizeof(T[le].next));
T[p].next[x]=le++;
}
p=T[p].next[x];
T[p].cnt++;
i++;
}
}
void query(char * s)
{
int i=0,p=0;
while(s[i])
{
int x = s[i]-'a';
if(T[p].next[x]==-1)
{
puts("0");
return ;
}
p=T[p].next[x];
i++;
}
printf("%d\n",T[p].cnt);
}
int main()
{
int n,m;
char str[20];
scanf("%d",&n);
le = 1;
T[0].cnt=0;
memset(T[0].next,-1,sizeof(T[0].next));
while(n--)
{
scanf("%s",str);
insert(str);
}
scanf("%d",&m);
while(m--)
{
scanf("%s",str);
query(str);
}
//cout < "Hello world!" << endl;
return 0;
}