题目(类似):
s = “3[a]2[bc]”, return “aaabcbc”.
s = “3[a2[c]]”, return “accaccacc”.
#include <iostream>
using namespace std;
string decode(string s, int &k) {
string res = "";
int num = 0;
while(k < s.size() ) {
if(s[k] >= 'a' && s[k] <= 'z') {
res += s[k];
k++;
}
if(s[k] >= '0' && s[k] <= '9') {
num = num*10+(s[k]-'0');
k++;
}
else if(s[k] == '[') {
k++;
string t = decode(s, k);
for(int i = 0; i < num; i++) {
res += t;
}
num = 0;
}
else if(s[k] == ']') {
k++;
return res;
}
}
return res;
}
int main(){
string input;
cin>>input;
int k = 0;
string res=decode(input,k);
for(int i=0;i<res.length();i++){
cout<<res[i];
}
cout<<endl;
}