/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Wrong Answer 1ms 324.0 KiB
#3 Wrong Answer 2ms 324.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define bug(a) cout<< #a << " : " << a <<endl;
#define bug2(a, b) cout<< #a << " : " << a  << " " << #b << " : " << b << endl;

void solve(int cs){
    int n,m;
    cin >> n >> m;
    string s; cin >> s;
    set<int> v[30];
    if(m >= n){
        sort(s.begin(),s.end());
        cout << s << '\n';
        return;
    }
    for(int i = 0; i < n; i++){
        v[s[i] - 'a'].insert(i);
    }
    string ans = "";
    int cur = 0;
    while(m and ans.size() < n){
        for(auto &u : v){
            if(u.size() == 0)continue;
            while(u.size()){
                int i = *u.begin();
                u.erase(u.begin());
                //bug(i);
                if(s[i] == s[cur]){
                ans += s[i];
                cur++;

                }else{
                    m--;
                    ans += s[i];
                    
                    v[s[cur] - 'a'].erase(cur);
                    v[s[cur] - 'a'].insert(i);
                    swap(s[i],s[cur]);
                }
            }
            
        }
    }
    int baki = n - ans.size();
    if(baki > 0 )ans += s.substr(ans.size(),baki);
    cout << ans << '\n';
    
}


int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);
    cout.tie(0);

    int tc = 1;
    cin >> tc;

    for(int cs = 1; cs <= tc; cs++){
        solve(cs);
    }
}

Information

Submit By
Type
Submission
Problem
P1058 Lexicographically Smallest String
Contest
Sylhet ICPC 2024 Collaborative Challenge: Episode 2
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-30 12:38:52
Judged At
2024-11-11 02:34:35
Judged By
Score
5
Total Time
2ms
Peak Memory
324.0 KiB