-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTrie_Data_Structure.cpp
More file actions
60 lines (59 loc) · 1.18 KB
/
Trie_Data_Structure.cpp
File metadata and controls
60 lines (59 loc) · 1.18 KB
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
#include <bits/stdc++.h>
using namespace std;
struct node
{
node *next[10];
bool endmark;
node()
{
for(int i = 0; i < 10; i++) next[i] = NULL;
endmark = false;
}
};
void Build_Trie (char *str , int len , node *curr)
{
for(int i = 0; i < len; i++)
{
int id = str[i] - '0';
if(curr->next[id] == NULL)
curr->next[id] = new node();
curr = curr->next[id];
}
curr->endmark = true;
}
bool searc (char *str , int len , node *curr)
{
for(int i = 0; i < len; i++)
{
int id = str[i] - '0';
if(curr->next[id] == NULL) return false;
curr = curr-> next[id];
}
return curr->endmark;
}
void del(node *cur) {
for(int i=0;i<10;i++)
if(cur->next[i])
del(cur->next[i]);
delete(cur);
}
int main()
{
char s[10001][13];
node *root = new node();
for(int i = 0; i < 5; i++)
{
scanf(" %s" , s[i]);
Build_Trie(s[i] , strlen(s[i]) , root);
}
int q;
cin >> q;
char s1[100];
for(int i =0 ; i < q; i++)
{
scanf(" %s", s1);
bool k = searc(s1 , strlen(s1) , root);
cout<< k << endl;
}
del(root);
}