• 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;
    }
  • 相关阅读:
    HDU-1205
    HDU-2033
    HDU-2032
    HDU-2031
    HDU-2030
    HDU-2029
    HDU-2028
    HDU-2027
    HDU-2026
    HDU-2025
  • 原文地址:https://www.cnblogs.com/lightmonster/p/10681900.html
Copyright © 2020-2023  润新知