-
Notifications
You must be signed in to change notification settings - Fork 67
/
ADAINDEX.cpp
134 lines (118 loc) · 2.61 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
//#include <unordered_map>
#include <utility>
#include <cassert>
#include <iomanip>
#include <ctime>
#include <fstream>
using namespace std;
const int me = 1000025;
template <class T> void FastInput(T &n){
char ch;
int sign = 1;
while(ch = getchar_unlocked(), isspace(ch)) {
};
n = 0;
if(ch == '-')
sign = -1;
else n = ch - '0';
while(ch = getchar_unlocked(), isdigit(ch))
n = (n << 3) + (n << 1) + ch - '0';
if(sign == -1)
n = ~n + 1;
}
template<class T> void FastPrint(T n){
if(n == 0){
puts("0");
return;
}
char buffer[256];
int ptr = 0, sign = 1;
if(n < 0){
sign = -1;
n = ~n + 1;
}
while(n > 0){
buffer[ptr ++] = (char)(n % 10 + '0');
n /= 10;
}
if(sign == -1)
putchar_unlocked('-');
for(int i = ptr - 1; i >= 0; i --)
putchar_unlocked(buffer[i]);
puts("");
}
const int ALPHABET = 26;
const char LOW = 'a';
struct node{
int count;
node *links[ALPHABET];
node() {
count = 0;
}
};
typedef node* pnode;
pnode Initialize(int value){
pnode item = (pnode)malloc(sizeof(node));
item->count = value;
for(int i = 0; i < ALPHABET; i ++)
item->links[i] = NULL;
return item;
}
void add(pnode &root, char *s, int length){
pnode now = root;
for(int i = 0; i < length; i ++){
int position = s[i] - LOW;
if(now->links[position] == NULL){
now->links[position] = Initialize(0);
}
now = now->links[position];
now->count ++;
}
}
int get(pnode &root, char *s, int length){
pnode now = root;
for(int i = 0; i < length; i ++){
int position = s[i] - LOW;
if(now->links[position] == NULL)
return 0;
now = now->links[position];
}
return now->count;
}
int N, Q;
char s[me];
int main() {
//ios_base::sync_with_stdio(0);
//cin.tie(0);
pnode root = Initialize(0);
scanf("%d%d", &N, &Q);
getchar();
while(N --){
int length = 0;
char ch;
while(ch = getchar(), ch != '\n')
s[length ++] = ch;
add(root, s, length);
}
while(Q --){
int length = 0;
char ch;
while(ch = getchar(), ch != '\n')
s[length ++] = ch;
printf("%d\n", get(root, s, length));
}
return 0;
}