/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 2ms 540.0 KiB
#3 Accepted 2ms 540.0 KiB
#4 Accepted 9ms 976.0 KiB
#5 Accepted 11ms 984.0 KiB
#6 Accepted 8ms 912.0 KiB
#7 Accepted 10ms 540.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];
           }
           int a=0;
           for(int j=0;j<mp[temp[0]];j++){
              if(temp[j]!=temp[0])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];
           }
            a=0;
           for(int j=0;j<mp[temp[0]];j++){
              if(temp[j]!=temp[0])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-19 09:56:19
Judged At
2024-05-19 09:56:19
Judged By
Score
100
Total Time
11ms
Peak Memory
984.0 KiB