Odpowiedź:
#include <iostream>
#include <string>
string unpack(string s) {
string unpacked;
int i = 0;
while (i < s.length()) {
if (s[i] >= '0' && s[i] <= '9') {
int sp = s.find('[', i); // start of PackedString
int ep = s.find(']', i); // end of PackedString
string str = s.substr(i, sp - i);
int n = stoi(str); // number of repetitions
str = s.substr(sp + 1, ep - sp - 1); // get the string
for (int j = 0; j < n; j++) {
unpacked += str; // add the string n times
}
i = ep + 1; // move i to the end of the string
}
else {
unpacked += s[i]; // add any other character
i++;
}
}
return unpacked;
}