Smart solution of decode String

using namespace std;
string decodeString(const string& s, int& i);
int main(void) {
    //3[z]2[2[y]pq4[2[jk]e1[f]]]ef
    string s = "3[z]2[2[y]pq4[2[jk]e1[f]]]ef";
    int i = 0;
    string out = decodeString(s, i);
    cout << out << endl;
    return 0;
}
string decodeString(const string& s, int& i) {
    string res;
    while (i<s.length()&&s[i]!=']')
    {
        if (!isdigit(s[i]))
            res += s[i++];
        else
        {
            int n = 0;
            while (i < s.length() && isdigit(s[i]))
                n = n * 10 + s[i++] - '0';
            i++;
            string t = decodeString(s, i);
            i++;
            while (n-- > 0)
            {
                res += t;
            }
        }
    }
    return res;
}
原文地址:https://www.cnblogs.com/lightmonster/p/10681900.html