-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADAINDEX - Ada and Indexing.cpp
69 lines (53 loc) · 1.57 KB
/
ADAINDEX - Ada and Indexing.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
#include<bits/stdc++.h>
using namespace std;
vector < string > v;
const int alphabetSize = 26;
struct node{
node* child[alphabetSize];
int cont[alphabetSize];
bool isWordFinished;
};
node* createNode(){
node* temp = new node;
for( int a_i=0; a_i<alphabetSize; a_i++ ) temp -> child[a_i] =NULL;
for( int a_i=0; a_i<alphabetSize; a_i++ ) temp -> cont[a_i] =0;
temp -> isWordFinished = false;
return temp;
}
void insertIt( node* root, string st ){
int a_i, b_i, s = st.size(), n, index;
node* tempR = root;
for( a_i=0; a_i<s; a_i++ ){
index = st[a_i] - 'a';
if( !tempR -> child[index] ){
tempR ->child[index] = createNode();
//cout<< tempR -> cont[index]<<endl;
}
tempR = tempR -> child[index];
tempR -> cont[index]++;
}
tempR -> isWordFinished = true;
}
int searchIt( node* tempR, string st ){
int a_i, b_i, s=st.size(), n, temp, index;
for( a_i=0; a_i<s; a_i++ ){
index = st[a_i] - 'a';
if( !tempR -> child[index] ) return 0;
tempR = tempR -> child[index];
}
//if( tempR -> isWordFinished ) return true;
return tempR -> cont[index];
}
int main(){
int a_i, b_i, n, temp, testCase, q;
string st, st1, st2;
cin>>n>>q;
v.resize( n );
for( a_i=0; a_i<n; a_i++ ) cin>> v[a_i];
node *root = createNode();
for( a_i=0; a_i<n; a_i++ ) insertIt( root, v[a_i] );
while( q-- ){
cin>>st2;
cout<<searchIt( root, st2 )<<endl;
}
}