/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 1ms 556.0 KiB
#3 Accepted 1ms 332.0 KiB
#4 Accepted 13ms 980.0 KiB
#5 Accepted 19ms 972.0 KiB
#6 Wrong Answer 15ms 880.0 KiB
#7 Wrong Answer 17ms 664.0 KiB
#8 Wrong Answer 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:03:39
Judged At
2024-05-20 10:03:39
Judged By
Score
40
Total Time
19ms
Peak Memory
980.0 KiB