/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 332.0 KiB
#2 Accepted 2ms 340.0 KiB
#3 Accepted 2ms 332.0 KiB
#4 Accepted 24ms 904.0 KiB
#5 Accepted 37ms 976.0 KiB
#6 Accepted 27ms 976.0 KiB
#7 Accepted 34ms 656.0 KiB
#8 Accepted 3ms 328.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:03:39
Judged At
2024-11-11 03:30:01
Judged By
Score
100
Total Time
37ms
Peak Memory
976.0 KiB