/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 536.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 532.0 KiB
#4 Accepted 2ms 580.0 KiB
#5 Accepted 7ms 952.0 KiB
#6 Accepted 4ms 684.0 KiB
#7 Accepted 5ms 832.0 KiB
#8 Accepted 3ms 832.0 KiB

Code

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


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
    cin>>t;
    while(t--){
       int n,k;
       cin>>n>>k;
       string s;
       cin>>s;
       vector<int>fre(26,0);
       int limit=0;
       int start=0;
       for(char ch:s)fre[ch-'a']++;
       for(char ch='a';ch < 'z' && k;ch++){
            if(!fre[ch-'a'])continue;
            limit+=fre[ch-'a'];
            vector<char>id;
            for(int j=start;j<limit&&k>0;j++){
                if(ch!=s[j]){
                    id.push_back(s[j]);
                    s[j]=ch;
                    k--;
                }
            }
            start=limit;
            if((int)id.size()==0)continue;
            sort(id.rbegin(),id.rend());
            int len=(int)id.size();
            int l=0;
            for(int j=n-1;j>=limit&&l<len;j--){
                if(s[j]==ch){
                    s[j]=id[l];
                    l++;
                }
            }

       }
       cout<<s<<"\n";

     

    }




    
   
   return 0;
 
}

Information

Submit By
Type
Submission
Problem
P1058 Lexicographically Smallest String
Language
C++20 (G++ 13.2.0)
Submit At
2024-05-09 17:55:42
Judged At
2024-12-17 11:30:28
Judged By
Score
100
Total Time
7ms
Peak Memory
952.0 KiB