| 18 | using namespace std; |
| 19 | |
| 20 | string decodeString(string s) { |
| 21 | stack<int> countStack; |
| 22 | stack<string> wordStack; |
| 23 | |
| 24 | int number = 0; |
| 25 | string word = ""; |
| 26 | for (int i = 0; i < s.length(); ++i) { |
| 27 | char ch = s[i]; |
| 28 | if (isdigit(ch)) { |
| 29 | number = number * 10 + (ch - '0'); |
| 30 | } |
| 31 | else if (isalpha(ch)) { |
| 32 | word += ch; |
| 33 | } |
| 34 | else if (s[i] == '[') { |
| 35 | wordStack.push(word); |
| 36 | countStack.push(number); |
| 37 | word = ""; |
| 38 | number = 0; |
| 39 | } |
| 40 | else { //If s[i] is ']' |
| 41 | int topMult = countStack.top(); countStack.pop(); |
| 42 | string topS = wordStack.top(); wordStack.pop(); |
| 43 | for (int i = 0; i < topMult; i++) { |
| 44 | topS.append(word); |
| 45 | } |
| 46 | word = topS; |
| 47 | } |
| 48 | } |
| 49 | return word; |
| 50 | } |
| 51 | |
| 52 | int main() { |
| 53 |