/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 1ms 320.0 KiB
#3 Accepted 3ms 580.0 KiB
#4 Accepted 506ms 9.961 MiB
#5 Accepted 866ms 9.953 MiB
#6 Accepted 576ms 9.836 MiB
#7 Accepted 623ms 2.449 MiB
#8 Accepted 11ms 604.0 KiB

Code

#include<bits/stdc++.h>
#define endl        '\n'
#define F           first
#define S           second
#define pb          push_back
#define yes         cout<<"YES\n"
#define no          cout<<"NO\n"
#define all(x)      x.begin(),x.end()
#define allr(x)     x.rbegin(),x.rend()
#define error1(x)   cerr<<#x<<" = "<<(x)<<endl
#define error2(a,b) cerr<<"("<<#a<<", "<<#b<<") = ("<<(a)<<", "<<(b)<<")\n";
#define coutall(v)  for(auto &it: v) cout << it << " "; cout << endl;
using namespace std;
using ll = long long;
using ld = long double;

void solve() {
    string s1, s2;
    cin >> s1;
    map<char, int> mp;
    for(auto &i: s1) {
        mp[i]++;
    }

    auto getAns = [&](string &s1, string &s2) -> ll {
        multiset<pair<char, char>> st1, st2;
        for(int i = 0; i < s1.size(); i++) {
            st1.insert({s1[i], s2[i]});
            st2.insert({s1[i], s2[i]});
        }
        ll oneOp = 0, moreOp = 0;
        map<char, ll> mpMoreOp;
        for(auto &[i, j]: st1) {
            auto it1 = st2.find({i, j});
            if(i == j or it1 == st2.end()) continue;
            auto it2 = st2.find({j, i});
            if(it2 == st2.end()) {
                // cout << i << " " << j << endl;
                moreOp++;
                mpMoreOp[i]++;
                st2.erase(it1);
            }
            else {
                oneOp++;
                st2.erase(it1);
                st2.erase(it2);
            }
        }
        // cout << oneOp << " " << moreOp << endl;
        return oneOp + (moreOp == 0 ? 0 : moreOp - mpMoreOp.begin()->S);
    };

    ll ans;
    if(mp.size() == 1) {
        ans = 0;
    }
    else if(mp.size() == 2) {
        char ch1 = mp.begin()->F, ch2 = next(mp.begin())->F;
        // AB
        s2 = "";
        for(int i = 0; i < mp[ch1]; i++) s2 += ch1;
        for(int i = 0; i < mp[ch2]; i++) s2 += ch2;
        // cout << s2 << " => " << getAns(s1, s2) << endl;
        ans = getAns(s1, s2);

        // BA
        s2 = "";
        for(int i = 0; i < mp[ch2]; i++) s2 += ch2;
        for(int i = 0; i < mp[ch1]; i++) s2 += ch1;
        // cout << s2 << " => " << getAns(s1, s2) << endl;
        ans = min(ans, getAns(s1, s2));
    }
    else {
        // ABC
        s2 = "";
        for(int i = 0; i < mp['A']; i++) s2 += 'A';
        for(int i = 0; i < mp['B']; i++) s2 += 'B';
        for(int i = 0; i < mp['C']; i++) s2 += 'C';
        // cout << s2 << " => " << getAns(s1, s2) << endl;
        ans = getAns(s1, s2);

        // ACB
        s2 = "";
        for(int i = 0; i < mp['A']; i++) s2 += 'A';
        for(int i = 0; i < mp['C']; i++) s2 += 'C';
        for(int i = 0; i < mp['B']; i++) s2 += 'B';
        // cout << s2 << " => " << getAns(s1, s2) << endl;
        ans = min(ans, getAns(s1, s2));

        // BAC
        s2 = "";
        for(int i = 0; i < mp['B']; i++) s2 += 'B';
        for(int i = 0; i < mp['A']; i++) s2 += 'A';
        for(int i = 0; i < mp['C']; i++) s2 += 'C';
        // cout << s2 << " => " << getAns(s1, s2) << endl;
        ans = min(ans, getAns(s1, s2));

        // BCA
        s2 = "";
        for(int i = 0; i < mp['B']; i++) s2 += 'B';
        for(int i = 0; i < mp['C']; i++) s2 += 'C';
        for(int i = 0; i < mp['A']; i++) s2 += 'A';
        // cout << s2 << " => " << getAns(s1, s2) << endl;
        ans = min(ans, getAns(s1, s2));

        // CAB
        s2 = "";
        for(int i = 0; i < mp['C']; i++) s2 += 'C';
        for(int i = 0; i < mp['A']; i++) s2 += 'A';
        for(int i = 0; i < mp['B']; i++) s2 += 'B';
        // cout << s2 << " => " << getAns(s1, s2) << endl;
        ans = min(ans, getAns(s1, s2));

        // CBA
        s2 = "";
        for(int i = 0; i < mp['C']; i++) s2 += 'C';
        for(int i = 0; i < mp['B']; i++) s2 += 'B';
        for(int i = 0; i < mp['A']; i++) s2 += 'A';
        // cout << s2 << " => " << getAns(s1, s2) << endl;
        ans = min(ans, getAns(s1, s2));
    }
    cout << ans << endl;
    return;
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case " << t << ": ";
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1059 String ABC
Language
C++20 (G++ 13.2.0)
Submit At
2024-05-20 10:44:34
Judged At
2024-11-11 03:29:59
Judged By
Score
100
Total Time
866ms
Peak Memory
9.961 MiB