-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbabynames.cpp
More file actions
221 lines (202 loc) · 6.51 KB
/
babynames.cpp
File metadata and controls
221 lines (202 loc) · 6.51 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Author: Maanas Karwa
* It is ok to share my code anonymously for educational purposes
* */
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct vertex {
vertex *child[26] = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
vertex *parent;
int numberOfWordsEndingInSubtree;
char alphabet;
bool exist;
explicit vertex(char a, vertex *p = nullptr) : alphabet(a), exist(false) {
parent = p;
numberOfWordsEndingInSubtree = 0;
}
};
class Trie {
public:
Trie() { root = new vertex('!'); }
vertex *root;
void insert(const string &word) { // insert a word into trie
vertex *cur = root;
for (char i: word) { // O(n)
int alphaNum = i - 'A';
if (cur->child[alphaNum] == nullptr) // add new branch if NULL
{
cur->child[alphaNum] = new vertex(i, cur);
}
cur->numberOfWordsEndingInSubtree++;
cur = cur->child[alphaNum];
}
cur->exist = true;
cur->numberOfWordsEndingInSubtree++;
}
bool deleteWord(const string &word) {
vertex *cur = root;
int alphaNum;
for (const char &i: word) { // O(n)
alphaNum = i - 'A';
if (cur->child[alphaNum] == nullptr)
return false;
cur = cur->child[alphaNum];
}
vertex *parent = cur->parent;
if (cur->numberOfWordsEndingInSubtree > 1) {
if (!cur->exist) {
return false;
}
cur->exist = false;
cur->numberOfWordsEndingInSubtree--;
} else {
parent->child[alphaNum] = nullptr;
delete cur;
}
while (parent) {
parent->numberOfWordsEndingInSubtree--;
if (parent->numberOfWordsEndingInSubtree < 0)
parent->numberOfWordsEndingInSubtree = 0;
parent = parent->parent;
}
return true;
}
};
/*
* Logic: for intervals, the left bound will have to be either a substring of right bound (eg ABC, ABCDE),
* or they will differ at a character within the min length of the two strings (eg ABD, AC)
*/
int findWordsInInterval(vertex *v, const string &l, const string &r) {
auto minLen = min(l.length(), r.length());
int ans = 0;
vertex *curL = v, *curR = v; //two pointer approach
int i = 0;
for (; i < minLen; ++i) {
int alphaL = l[i] - 'A', alphaR = r[i] - 'A';
if (alphaL == alphaR) {
curL = curL->child[alphaL];
curR = curR->child[alphaR];
if (curL == nullptr) { //no words in this interval since trie does not have element
return 0;
}
} else {
// at the first point of diff, both l and r will be the same.
// we want to include all the strings between l and r, r exclusive
// first count everything from l+1 to r-1 inclusive
for (int j = alphaL + 1; j < alphaR; j++) {
auto child = curL->child[j];
if (child)
ans += child->numberOfWordsEndingInSubtree;
}
// more chars left in left string
if (curL && curL->child[alphaL]) {
curL = curL->child[alphaL];
int iNew = i + 1;
if (iNew < l.length() - 1) {
// count stuff after the chars start being diff but to the right.
// eg for bound ABCD to AF, i want to count all strings of form ABCE..., ABCF... etc
for (; iNew < l.length() - 1 && curL; iNew++) {
alphaL = l[iNew] - 'A';
for (int j = alphaL + 1; j < 26; j++)
if (curL->child[j])
ans += curL->child[j]->numberOfWordsEndingInSubtree;
curL = curL->child[alphaL];
}
// also want to count ABCD... all
alphaL = l[l.length() - 1] - 'A';
if (curL && curL->child[alphaL]) {
ans += curL->child[alphaL]->numberOfWordsEndingInSubtree;
}
} else { //string is over, eg AC, ADP
//we want to count all of C's children
ans += curL->numberOfWordsEndingInSubtree;
}
}
// eg AB to AFBP
// want to count AFA..., AFBA... to AFBO...
// things that share some substring with right bound but not the whole thing
if (curR && curR->child[alphaR]) {
curR = curR->child[alphaR];
for (int iNew = i + 1; iNew < r.length() - 1 && curR; iNew++) {
alphaR = r[iNew] - 'A';
for (int j = alphaR - 1; j >= 0; j--)
if (curR->child[j])
ans += curR->child[j]->numberOfWordsEndingInSubtree;
curR = curR->child[alphaR];
}
}
return ans;
}
}
// if we reached this point, l is a substring of r. otherwise, we would have returned ans already
assert(curL == curR);
// eg AB, ABDF
if (r.length() > minLen && curR) {
ans += curR->exist; // want to include AB if it exists
// also want to include ABA... to ABC..., ABDA... to ABDE..., etc
int alphaR = r[minLen] - 'A';
for (int j = 0; j < alphaR; j++) {
if (curR->child[j])
ans += curR->child[j]->numberOfWordsEndingInSubtree;
}
if (curR->child[alphaR]) {
curR = curR->child[alphaR];
for (; i < r.length() - 1 && curR; i++) {
alphaR = r[i] - 'A';
for (int j = alphaR - 1; j >= 0; j--)
if (curR->child[j])
ans += curR->child[j]->numberOfWordsEndingInSubtree;
curR = curR->child[alphaR];
}
}
}
return ans;
}
Trie male, female;
int commandNumber, gender;
string name, l, r;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin.exceptions(std::istream::failbit);
while (true) {
cin >> commandNumber;
if (!commandNumber)
break;
switch (commandNumber) {
case 1:
cin >> name >> gender;
if (gender == 1) {
male.insert(name);
} else {
female.insert(name);
}
break;
case 2:
cin >> name;
if (!male.deleteWord(name))
female.deleteWord(name);
break;
case 3: {
cin >> l >> r >> gender;
int ans;
if (gender == 0) {
ans = findWordsInInterval(male.root, l, r) + findWordsInInterval(female.root, l, r);
} else if (gender == 1) {
ans = findWordsInInterval(male.root, l, r);
} else {
ans = findWordsInInterval(female.root, l, r);
}
cout << ans << "\n";
break;
}
default:
return 2;
}
}
}
#undef ll
#undef u