题目:字符串解码
使用到的是栈和递归
关于为什么是遇到‘【’就继续入栈:这是为了处理前置的字母,可以将其直接拼接到res的尾部
1string decodeString(string s)
2{
3 std::stack<std::pair<string, int>> stk;
4 string res;
5 int k = 0;
6 for(char c : s)
7 {
8 if(isalpha(c)) res += c;
9 else if(isdigit(c)) k = k * 10 + (c - '0');
10 else if(c == '[')
11 {
12 stk.emplace(std::move(res), k);
13 k = 0;
14 }
15 else
16 {
17 auto [pre_res, pre_k] = stk.top();
18 stk.pop();
19 while(pre_k--)
20 {
21 pre_res += res;
22 }
23 res = std::move(pre_res);
24 }
25 }
26 return res;
27}