【題解】Codeforces 1389C. Good String

【題目敘述】http://codeforces.com/contest/1389/problem/C

#include <iostream>
#include <cstring>
using namespace std;
 
int t, l, cnt[10], mx;
string s;
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> t;
    while (t--){
        cin >> s;
        for (int i = 0; i < 10; i++){
            cnt[i] = 0;
        }
        l = s.length();
        for (int i = 0; i < l; i++){
            cnt[s[i]-'0']++;
        }
        mx = 0;
        for (int i = 0; i < 10; i++){
            mx = max(mx, cnt[i]);
        }
        for (int i = 0; i < 10; i++){
            if (cnt[i]*2 < mx) continue;
            for (int j = 0; j < 10; j++){
                if (j == i) continue;
                if (cnt[j]*2 < mx) continue;
                bool b = true;
                int cnt = 0;
                for (int k = 0; k < l; k++){
                    if (b && s[k]-'0' == i){
                        b = false;
                    }
                    else if (!b && s[k]-'0' == j){
                        cnt++;
                        b = true;
                    }
                }
                mx = max(mx, cnt*2);
            }
        }
        cout << l-mx << "\n";
    }
}
分享本文 Share with friends