-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContacts.cpp
More file actions
51 lines (48 loc) · 1.17 KB
/
Contacts.cpp
File metadata and controls
51 lines (48 loc) · 1.17 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
#include <iostream>
#include <string>
#include <map>
using namespace std;
struct Node{
int count=0;
map<char, Node*> nextMap;
};
int main()
{
int n;
cin >> n;
Node *head = new Node();
string add = "add", find = "find";
for(int i=0;i<n;i++){
string action, name;
cin>>action>>name;
if(action == add){
long len = name.size();
Node *cur = head;
for(int i=0;i<len;i++){
Node *next = cur->nextMap[name[i]];
if(next == NULL){
next = new Node();
cur->nextMap[name[i]]=next;
}
next->count++;
cur = cur->nextMap[name[i]];
}
}
else{
int res = 0;
long len = name.size();
Node *cur = head;
for(int i=0;i<len;i++){
Node *next = cur->nextMap[name[i]];
if(next == NULL){
res = 0;
break;
}
cur = cur->nextMap[name[i]];
res = cur->count;
}
cout<<res<<endl;
}
}
return 0;
}