-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem77.cpp
More file actions
60 lines (50 loc) · 1.09 KB
/
problem77.cpp
File metadata and controls
60 lines (50 loc) · 1.09 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
#include <bits/stdc++.h>
using namespace std;
class SecretDecoder {
string s;
int idx = 0; // pointer to current index in the string
public:
void readInput() {
cin >> s;
if (s.empty() || s.length() > 1e4) {
cout << "!! Invalid Input !!" << endl;
exit(1);
}
}
string decode() {
return decodeHelper();
}
private:
string decodeHelper() {
string result = "";
while (idx < s.length() && s[idx] != ']') {
if (isdigit(s[idx])) {
int k = 0;
// Extract full number (could be more than 1 digit like 10[abc])
while (isdigit(s[idx])) {
k = k * 10 + (s[idx] - '0');
idx++;
}
idx++; // skip the '['
string decodedSubstring = decodeHelper();
idx++; // skip the ']'
// Append repeated string k times
while (k--) result += decodedSubstring;
}
else {
result += s[idx++];
}
}
return result;
}
public:
void display() {
cout << decode() << endl;
}
};
int main() {
SecretDecoder sd;
sd.readInput();
sd.display();
return 0;
}