Home>

I don't know where my code is wrong because of the following problem.
Only 2 cases will not pass.

Even character strings used the fact that the index of the last character of each alphabet becomes odd when sorted.

Thank you.

Problem links

# include<bits/stdc ++. h>
#define rep (i, n) for (int i = 0;i<(int) (n);++ i)
using namespace std;
typedef long long ll;

int main () {
    string s;
    cin >>s;
    int n = s.length ();
    for (int i = 1;i<n;++ i) {
        string ss = s.erase (n-i);
        sort (ss.begin (), ss.end ());
        int cnt = 0;
        for (char w = 'a';w<= 'z';++ w) {
            if (s.find (w)! = string :: npos) {
                int last = ss.find_last_of (w);
                cout<<"ss ="<<ss<<"last ="<<last<<endl;
                if (last% 2 == 0) ++ cnt;
            }
        }
        if (cnt == 0) {
            cout<<s.length ()<<endl;
            return 0;
        }
    }
    return 0;
}
  • Answer # 1

    I don't think you know exactly what the even string is.
    For example, if you look at "aabbaabb", it is an even character string with two "aabb" itself, but "aabbaa" and "aabb" are not even strings but only "aa" has two "a" s. An even string.

      

    Even character strings used the fact that the index of the last character of each alphabet becomes odd when sorted.

    In other words, this means that all characters appear an even number of times, so non-even character strings such as "aabbaa" and "aabb" are also judged as even character strings.