-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidx39.hpp
315 lines (263 loc) · 7.75 KB
/
idx39.hpp
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#pragma once
#include "list.hpp"
#include "subs.hpp"
#include "strs.hpp"
#include "graph.hpp"
namespace LCIndex39 {
/**
///////////// 391.
*/
/**
///////////// 392.
*/
/**
///////////// 393.
*/
/**
///////////// 394. Decode String
Given an encoded string, return its decoded string.
The encoding rule is: k[encoded_string],
where the encoded_string inside the square brackets is being repeated exactly k times.
Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; No extra white spaces,
square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not contain any digits and that
digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4].
Examples:
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
*/
std::string decodeString(std::string s) {
std::string res;
enum {
TEXT = 0,
DIGITAL,
LEFT,
} st;
std::stack<int> ts;
std::stack<std::string> tss;
size_t digital_l = 0;
size_t digital_r = 0;
std::string str;
st = TEXT;
for (size_t i = 0; i < s.size(); ++i) {
char c = s[i];
if (st == TEXT) {
if (isdigit(c)) {
digital_l = i;
st = DIGITAL;
} else {
res += c;
}
} else if (st == DIGITAL) {
if (isdigit(c)) {
// still
} else if (c == '[') {
digital_r = i;
ts.emplace(std::stoi(s.substr(digital_l, digital_r - digital_l)));
st = LEFT;
}
} else if (st == LEFT) {
if (c == ']') {
if (ts.empty()) {
// error
return "";
}
std::string section;
if (!tss.empty()) {
section = tss.top();
tss.pop();
}
for (int j = 0; j < ts.top(); ++j) {
section += str;
}
ts.pop();
if (!ts.empty()) {
str = std::move(section);
} else {
res += section;
str.clear();
st = TEXT;
}
} else if (isdigit(c)) {
tss.emplace(str);
str.clear();
digital_l = i;
st = DIGITAL;
} else {
str += c;
}
}
}
return res;
}
FTEST(test_decodeString) {
auto t = [&](const std::string& s) {
auto re = decodeString(s);
LOG(INFO) << s << " decode: " << re;
return re;
};
FEXP(t(""), "");
FEXP(t("a"), "a");
FEXP(t("ab"), "ab");
FEXP(t("abc"), "abc");
FEXP(t("1[]"), "");
FEXP(t("2[]"), "");
FEXP(t("3[]"), "");
FEXP(t("1[ ]"), " ");
FEXP(t("2[ ]"), " ");
FEXP(t("3[ ]"), " ");
FEXP(t("1[a]"), "a");
FEXP(t("2[a]"), "aa");
FEXP(t("3[a]"), "aaa");
FEXP(t("1[ab]"), "ab");
FEXP(t("2[ab]"), "abab");
FEXP(t("3[ab]"), "ababab");
FEXP(t("1[abc]"), "abc");
FEXP(t("2[abc]"), "abcabc");
FEXP(t("3[abc]"), "abcabcabc");
FEXP(t("3[a]2[bc]"), "aaabcbc");
FEXP(t("3[2[c]]"), "cccccc");
FEXP(t("3[2[1[cb]]]"), "cbcbcbcbcbcb");
FEXP(t("3[a2[c]]"), "accaccacc");
FEXP(t("3[2[c]a]"), "ccaccacca");
FEXP(t("3[a2[c]b]"), "accbaccbaccb");
FEXP(t("2[abc]3[cd]ef"), "abcabccdcdcdef");
}
/**
///////////// 395. Longest Substring with At Least K Repeating Characters
Find the length of the longest substring T of a given string
(consists of lowercase letters only) such that every character in T appears no less than k times.
Example 1:
Input:
s = "aaabb", k = 3
Output:
3
The longest substring is "aaa", as 'a' is repeated 3 times.
Example 2:
Input:
s = "ababbc", k = 2
Output:
5
The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
THOUGHTS:
sliding window
Try to find count of different chars is i(1~26) that at least K in target string.
Using 'count of char' to check if it's the expect, note that 'unique' or 'lek' changing is
'level trigger', means this happend when 'count of char' come to 0.
divide and conquer
1. count each char appears count from left to right
2. find the first appears count less than k, then split to left half.
1-2 recusive.
k=3 a b a b b c d c a b a
l i j len
cnts ↓ (cnts[a]>=3)
a 4 (cnts[a]>=3) (cnts[c or d]<3)
b 4
c 2 split to std::max(longest(l, i), longest(j, len))
d 1
*/
int longestSubstringAtLeast(std::string s, int k) {
int len = s.size();
int res = 0;
auto sliding_window_method = [&] {
int counts[26] = {0};
for (int cnt = 1; cnt <= 26; ++cnt) {
int unique = 0; // count of different char
int lek = 0; // count of char which count at least K
int start = 0; // start is window left border, i is right border
for (int i = 0; i < len;) {
if (cnt >= unique) { // window right expand
int idx = s[i] - 'a';
if (counts[idx]++ == 0) {
++unique;
}
if (counts[idx] == k) {
++lek;
}
++i;
} else { // more than cnt chars in window, left shrink
int idx = s[start] - 'a';
if (counts[idx]-- == k) {
--lek;
}
if (counts[idx] == 0) {
--unique;
}
++start;
}
if (unique == cnt && unique == lek) {
// all unique count of number appear more than k
res = std::max(res, i - start);
}
}
}
return res;
};
auto divide_conquer_method = [&] {
std::function<int(int, int)> r_func;
r_func = [&](int start, int end) {
int counts[26] = {0};
for (int i = start; i < end; ++i) {
counts[s[i] - 'a']++;
}
int i = start;
while (i < end && counts[s[i] - 'a'] >= k) {
++i;
}
if (i == end) {
return end - start;
}
int j = i + 1;
while (j < end && counts[s[j] - 'a'] < k) {
++j;
}
return std::max(r_func(start, i), r_func(j, end));
};
return r_func(0, len);
};
return divide_conquer_method();
}
FTEST(test_longestSubstringAtLeast) {
auto t = [&](const std::string& s, int k) {
auto re = longestSubstringAtLeast(s, k);
LOG(INFO) << s << " " << k << " as least substr lenght: " << re;
return re;
};
FEXP(t("", 0), 0);
FEXP(t("", 1), 0);
FEXP(t("a", 1), 1);
FEXP(t("a", 2), 0);
FEXP(t("aa", 3), 0);
FEXP(t("aa", 2), 2);
FEXP(t("aba", 2), 0);
FEXP(t("baa", 2), 2);
FEXP(t("aab", 2), 2);
FEXP(t("aaa", 2), 3);
FEXP(t("abaa", 3), 0);
FEXP(t("aaba", 3), 0);
FEXP(t("aaa", 3), 3);
FEXP(t("aaab", 3), 3);
FEXP(t("baaa", 3), 3);
FEXP(t("aaabb", 3), 3);
FEXP(t("bbaaa", 3), 3);
FEXP(t("bbbaaa", 3), 6);
FEXP(t("ababbc", 2), 5);
}
/**
///////////// 396.
*/
/**
///////////// 397.
*/
/**
///////////// 398.
*/
/**
///////////// 399.
*/
/**
///////////// 400.
*/
}