-
Notifications
You must be signed in to change notification settings - Fork 2
/
Trie.cpp
178 lines (162 loc) · 3.81 KB
/
Trie.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* -------------------------------- */
/* Name: MD. Khairul Basar */
/* Institute: HSTU */
/* Dept: CSE */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */
#include <bits/stdc++.h>
/* all header files */
#define fs first
#define sc second
#define sp printf(" ")
#define nl printf("\n")
#define pb(a) push_back(a)
#define setzero(a) memset(a,0,sizeof(a))
#define setneg(a) memset(a,-1,sizeof(a))
#define setinf(a) memset(a,126,sizeof(a))
#define tc1(x) printf("Case %d: ",x)
#define tc2(x) printf("Case #%d: ",x)
#define tc3(x) printf("Case %d:\n",x)
#define tc4(x) printf("Case #%d:\n",x)
#define iin(x) scanf("%d",&x)
#define din(x) scanf("%lf",&x)
#define lin(x) scanf("%lld",&x)
#define clin(x) scanf("%I64d",&x)
#define pr1(x) cout<<x<<"\n"
#define pr2(x,y) cout<<x<<" "<<y<<"\n"
#define pr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<"\n"
#define pr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<"\n"
#define fast ios::sync_with_stdio(0)
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define prflag1(flag) printf("%s\n",(flag)?"YES":"NO")
#define prflag2(flag) printf("%s\n",(flag)?"Yes":"No")
#define prflag3(flag) printf("%s\n",(flag)?"yes":"no")
/* macro definitions */
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
/* type definitions */
LL MOD_EXPO(LL b, LL p, LL m)
{
if(p==0) return 1;
LL ret=MOD_EXPO(b,p/2,m)%m;
ret=(ret*ret)%m;
return ((p&1) ? (ret*b)%m : ret%m);
}
LL POWER(LL N, LL K)
{
LL i,ans=1;
for(i=1; i<=K; i++) ans*=N;
return ans;
}
int SET(int N, int pos)
{
return (N | (1<<pos));
}
int RESET(int N, int pos)
{
return (N & !(1<<pos));
}
int CHECK(int N, int pos)
{
return (N & (1<<pos));
}
/* necessary functions */
int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction arrays */
int tc=1;
const double eps=1e-9;
const double pi=acos(-1.0);
const long long int mx=1e5;
const long long int mod=1e9+7;
/* global declarations */
struct node
{
bool end_mark;
node *next[26+1];
node()
{
end_mark=false;
for(int i=0; i<26; i++) next[i]=NULL;
}
};
node *root;
void insert_node(string str)
{
int i,id;
node *cur=root;
for(i=0; i<str.size(); i++)
{
id=str[i]-'a';
if(cur->next[id]==NULL) cur->next[id]=new node();
cur=cur->next[id];
}
cur->end_mark=true;
return;
}
bool search_node(string str)
{
int i,id;
node *cur=root;
for(i=0; i<str.size(); i++)
{
id=str[i]-'a';
if(cur->next[id]==NULL) return false;
cur=cur->next[id];
}
return cur->end_mark;
}
void delete_tree(node *cur)
{
int i;
for(i=0; i<26; i++)
{
if(cur->next[i]) delete_tree(cur->next[i]);
}
delete cur;
}
int main()
{
string str;
int q;
root=new node();
while(1)
{
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Exit\n");
cin>>q;
if(q==1)
{
cin>>str;
insert_node(str);
}
if(q==2)
{
cin>>str;
if(search_node(str)) pr1("Found");
else pr1("Not Found");
}
if(q==3)
{
delete_tree(root);
break;
}
}
return 0;
}