-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contacts.cpp
78 lines (75 loc) · 1.73 KB
/
Contacts.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
#include<bits/stdc++.h>
using namespace std;
class node{
public:
char data;
// bool flag = false;
// unordered_map<char, node*> child;
vector<node*> child;''
int count = 0;
node(char data){
this->data = data;
for(int i= 0;i<26;i++){
this->child.push_back(NULL);
}
}
};
class trie{
public:
node *root;
trie(){
root = new node(' ');
}
void insert(string contact){
node *temp = root;
int i = 0;
while(i < contact.size()){
if(temp->child[contact[i] - 'a'] == NULL){
temp->child[contact[i] - 'a'] = new node(contact[i]);
}
temp = temp->child[contact[i] - 'a'];
temp->count += 1;
i++;
}
// temp->flag = true;
}
int find(string contact){
node *temp = root;
int i = 0;
while(i < contact.size()){
if(temp->child[contact[i] - 'a'] == NULL){
return 0;
}
temp = temp->child[contact[i]-'a'];
i++;
}
// int count = 0;
// solve(temp, count);
// return count;
return temp->count;
}
// void solve(node *temp, int &count){
// if(temp == NULL)
// return ;
// if(temp->flag)
// count++;
// for(auto i : temp->child){
// solve(i, count);
// }
// }
};
int main(int argc, char const *argv[])
{
trie obj;
int n;
cin>>n;
for(int i = 0;i<n;i++){
string cho, ans;
cin>>cho>>ans;
if(cho == "add")
obj.insert(ans);
else
cout<<obj.find(ans)<<endl;
}
return 0;
}