-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map_Pair_sum_Trie.cpp
59 lines (53 loc) · 1.44 KB
/
Map_Pair_sum_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
class MapSum {
public:
/** Initialize your data structure here. */
struct node{
char x;
bool ends = false;
map< char, struct node* > m;
int sum =0;
};
struct node * head =NULL;
set< string > dup;
MapSum() {
head = new struct node();
}
void insert(string key, int val) {
int sz = key.size();
int fl =0;
struct node * start = head;
if(dup.find(key)== dup.end()){
dup.insert(key);
}
else fl =1;
for(int u=0;u<sz;u++){
if(start->m.find(key[u])==start->m.end()){
struct node * newt = new struct node();
newt->x = key[u];
newt->sum =val;
start->m[key[u]]= newt;
start = newt;
}else{
start = start->m[key[u]];
if(fl) start->sum= val;
else start->sum +=val;
}
}
start->ends = true;
}
int sum(string prefix) {
int sz= prefix.size();
struct node * s = head;
for(int u=0;u<sz;u++){
if(s->m.find(prefix[u])!= s->m.end()) s = s->m[prefix[u]];
else return 0;
}
return s->sum;
}
};
/**
* Your MapSum object will be instantiated and called as such:
* MapSum* obj = new MapSum();
* obj->insert(key,val);
* int param_2 = obj->sum(prefix);
*/