• + 0 comments

    wow very misleading question.

    spent alot of time, in vain, using the standard dynamic programming analysis, got no where because the choices of reduction for a string cant seem to be reduced to choices of reduction for its substrings, so the problem cant seem to be analyzed in terms of its subproblems - irreducible problem.

    turns out the problem just requires finding the parity invariant under reductions, since any non constant string can be reduced to length 1 or 2 which have different parity composition.

    O(n) no dynamic programming. very stupid question, why is it in dynamic programming when its just a trick question?

    int stringReduction(int n, const string& s) {
        vector<int> occurrence(3);
        for (char c : s) occurrence[c - 'a']++;
        if (occurrence[0] == n or occurrence[1] == n or occurrence[2] == n) return n;
        if (occurrence[0] % 2 == occurrence[1] % 2 and occurrence[1] % 2 == occurrence[2] % 2) return 2;
        else return 1;
    }
    
    int main()
    {
        int t;
        string s;
        cin >> t;
        for (int i=1; i <= t; i++) {
            cin >> s;
            cout << stringReduction(s.size(), s) << '\n';
        }
    }