Editorial for Chơi với "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.

\(\color{red}{\text{Spoiler Alert}_{{}_{{}^{{}^{v2.0}}}}}\)

\(\color{red}{\text{Khuyến khích bạn đọc trước khi đọc phần lời giải xin hãy thử code ra thuật của mình dù nó có sai hay đúng}}\)

\(\color{red}{\text{Sau đó từ phần bài giải và thuật toán trước đó mà đối chiếu, rút nhận xét với thuật của mình và thu được bài học (không lãng phí thời gian đâu).}}\)



\(\color{orange}{\text{Hint 1 <Brute-force>}}\)

  • Thử xóa từng cặp kí tự bằng nhau và tăng biến dếm

Nếu sau quá trình trên biến đếm lẻ thì Henry thắng và in "YES" ngược lại in ra "NO"


\(\color{orange}{\text{Hint 2 <Data-Structure>::<Stack>}}\)

  • Duyệt từng kí tự \(c\) của xâu

Nếu như \(Stack.top() \neq c\) thì ta thêm phần tử đó vào đầu

Nếu như \(Stack.top() = c\) thì ta xóa 2 kí tự bằng cách xóa \(Stack.top()\) và không chèn phần tử \(c\) vào


\(\color{green}{\text{Preference AC Code }}\): Data-structure::Stack

\(^{^{\color{purple}{\text{Complexity : }} O(n)\ \color{purple}{\text{time}}\ ||\ O(n)\ \color{purple}{\text{memory}}}}\)

C++
int main()
{
    string a;
    cin >> a;

    deque<char> S;
    bool win = false;
    for (char c : a)
    {
        if (!S.empty() && S.back() == c)
        {
            S.pop_back();
            win = !win;
        }
        else 
            S.push_back(c);

    }

    cout << (win ? "Yes" : "No");
    return 0;
}

\(\color{green}{\text{Preference AC Code }}\): Data-structure::Stack, Online Solving

\(^{^{\color{purple}{\text{Complexity : }} O(n)\ \color{purple}{\text{time}}\ ||\ O(n)\ \color{purple}{\text{memory}}}}\)

C++
bool isLowerLatin(char c) { return 'a' <= c && c <= 'z'; }
int main()
{
    bool win = false;
    deque<char> S;
    for (char c; isLowerLatin(c = getchar()); )
    {
        if (S.empty() || S.back() != c)
        {
            S.push_back(c);
            win ^= true;
        }
        else
            S.pop_back();
    }

    cout << (win ? "Yes" : "No");
    return 0;
}


Comments

There are no comments at the moment.