/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 1ms 540.0 KiB
#3 Accepted 1ms 540.0 KiB
#4 Accepted 13ms 980.0 KiB
#5 Accepted 20ms 984.0 KiB
#6 Accepted 14ms 896.0 KiB
#7 Accepted 18ms 772.0 KiB
#8 Accepted 2ms 540.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;
const long long M=2e6+10,MOD=1000000007;
typedef long long ll;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--){
       string S;
       cin>>S;
       int n=S.size();
       map<char,int>mp;
       for(char ch:S)mp[ch]++;
       int ans=n;
       for(char ch='A';ch<='C';ch++){
           if(mp[ch]==0)continue;
           int l=mp[ch];
           if(l==n){
            ans=0;
            break;
           }
           string p=S;
           string cur="";
           int cnt=0;
           for(int i=0;i<l;i++){
             if(p[i]!=ch)cnt++,cur+=p[i];
           }
           sort(cur.begin(),cur.end());
           string temp="";
           int pos=0;
           for(int j=l;j<n;j++){
               if(p[j]==ch)temp+=cur[pos],pos++;
               else temp+=p[j];
           }
           set<char>st;
           for(char c:temp)st.insert(c);
           for(auto it:st){
            int a=0;
           for(int j=0;j<mp[it];j++){
              if(temp[j]!=it)a++;
           }
           ans=min(ans,cnt+a);
           }
           reverse(cur.begin(),cur.end());
           temp="";
           pos=0;
           for(int j=l;j<n;j++){
               if(p[j]==ch)temp+=cur[pos],pos++;
               else temp+=p[j];
           }
           st.clear();
           for(char c:temp)st.insert(c);
           for(auto it:st){
            int a=0;
           for(int j=0;j<mp[it];j++){
              if(temp[j]!=it)a++;
           }
            ans=min(ans,cnt+a);
       }
          
       }
       cout<<ans<<"\n";

    }
    
   
   return 0;
}
 

Information

Submit By
Type
Submission
Problem
P1059 String ABC
Language
C++20 (G++ 13.2.0)
Submit At
2024-05-20 10:23:52
Judged At
2024-05-20 10:23:52
Judged By
Score
100
Total Time
20ms
Peak Memory
984.0 KiB