Editorial for Giải nén xâu


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Spoiler Alert


Hint 1

Lặp lại quá trình nhận số x và kí tự c tiếp sau nó

Xuất x lần kí tự c

Lưu ý kiểm tra trường hợp không nhận được số x nào (c là kí tự đơn lẻ* thì xuất c


Reference AC code | O(n) time | O(n) auxiliary space | String

C++
inline bool isDigit(char c) { return '0' <= c && c <= '9'; }
int main()
{
    string s;
    getString(s);

    for (int i = 0; i < sz(s); ++i)
    {
        if (isDigit(s[i]) == false) { cout << s[i]; continue; }
        int x = 0;
        while (i < sz(s) && isDigit(s[i])) x = 10 * x + (s[i++] - '0');
        while (x--) cout << s[i];
    }
    return 0;
}

Reference AC code | O(n) time | O(1) auxiliary space | String, Online Solving

C++
inline bool isLowLatin(char c) { return 'a' <= c && c <= 'z'; }
inline bool isDigit(char c)    { return '0' <= c && c <= '9'; }
int main()
{
    for (char c = getchar(); isLowLatin(c) || isDigit(c); c = getchar())
    {
        if (isDigit(c) == false) { cout << c; continue; }
        int x = 0;
        while (isDigit(c)) x = 10 * x + (c - '0'), c = getchar();
        while (x--) cout << c;
    }
    return 0;
}


Comments

There are no comments at the moment.