/ SeriousOJ /

Record Detail

Wrong Answer


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

Code

///******* In the name of ALLAH, Who is Most Gracious, Most Merciful *******///
///******* There is no God but ALLAH, MUHAMMAD(S.A.W) is the Messenger of ALLAH. *******///
///******* Every soul shall taste death.*******///

//  AL-HAMDULILLAH    //  AL-HAMDULILLAH     //  AL-HAMDULILLAH    //  AL-HAMDULILLAH
//  AL-HAMDULILLAH    //  AL-HAMDULILLAH     //  AL-HAMDULILLAH    //  AL-HAMDULILLAH
//  AL-HAMDULILLAH    //  AL-HAMDULILLAH     //  AL-HAMDULILLAH    //  AL-HAMDULILLAH
//  AL-HAMDULILLAH    //  AL-HAMDULILLAH     //  AL-HAMDULILLAH    //  AL-HAMDULILLAH
//  AL-HAMDULILLAH    //  AL-HAMDULILLAH     //  AL-HAMDULILLAH    //  AL-HAMDULILLAH

/**************************************************************************************
    ___  _          _   _  ___ ___  ________ _   _ _    _____ _     _      ___  _   _
   / _ \| |        | | | |/ _ \|  \/  |  _  | | | | |  |_   _| |   | |    / _ \| | | |
  / /_\ | |  ____  | |_| / /_\ | .  . | | | | | | | |    | | | |   | |   / /_\ | |_| |
  |  _  | | |____| |  _  |  _  | |\/| | | | | | | | |    | | | |   | |   |  _  |  _  |
  | | | | |____    | | | | | | | |  | | |/ /| |_| | |____| |_| |___| |___| | | | | | |
  \_| |_\_____/    \_| |_\_| |_\_|  |_|___/  \___/\_____\___/\_____\_____\_| |_\_| |_/

***************************************************************************************/

//  AL-HAMDULILLAH    //  AL-HAMDULILLAH     //  AL-HAMDULILLAH    //  AL-HAMDULILLAH
//  AL-HAMDULILLAH    //  AL-HAMDULILLAH     //  AL-HAMDULILLAH    //  AL-HAMDULILLAH
//  AL-HAMDULILLAH    //  AL-HAMDULILLAH     //  AL-HAMDULILLAH    //  AL-HAMDULILLAH


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<string.h>
#include<math.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>

#define 	ll              long long
#define 	size1           1000001
#define 	bm              1000000007   //bigmod
#define		reset(arr,n,i)  fill(arr,arr+n,i)  //cbrt(n) means cube root
#define		rset(arr,x)  	memset(arr,x,sizeof(arr))
#define		_ceil(x,y)      ((x)/(y))+(((x)%(y))>0)
#define		INF             (ll)1e18 + 10
#define		mod             1e9 + 7//998244353;
#define 	endl            "\n"

#define		_cpy(c,a,n)     for(int i=0;i<n;i++) c[i]=a[i];
#define 	_iin(a,n)       int a[n]; for(int i=0;i<n;i++) cin>>a[i]
#define 	_lin(a,n)       ll a[n]; for(int i=0;i<n;i++) cin>>a[i]
#define 	_in(a,n)        for(int i=0;i<n;i++) cin>>a[i]
#define 	_out(a,n)       for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl
#define		_celi(x,y)      (x+y-1)/(y)  //use this version of _ceil [note: ceil is risky]
#define		eps             0.0000000001

#define 	fopr()          freopen("input.txt", "r", stdin)
#define 	fopw()          freopen("output.txt", "w", stdout)
#define 	FastIO          ios_base::sync_with_stdio(false), cin.tie(0)

// Math :
void		mns(int *x, int *y)		{
	if(*x>=*y) *x-=*y, *y=0;
	else *y-=*x, *x=0;
}
int			log_2(ll n)				{
	int cnt=0;
	while(n/2) cnt++, n/=2;
	return cnt;
}
int			log2_(ll i)				{
	return i ? __builtin_clzll(1) - __builtin_clzll(i) : -1;
}
// Debuging :
#define		_dbg(_x,_y,_z)  cout<<_x<<" "<<_y<<" "<<_z<<endl
#define		_dbl			cout<<"_________________ "<<endl
#define		_db(_x,_y)      cout<<_x<<" "<<_y<<endl
#define     OK              cout<<"Ok"<<endl;

using namespace std;
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;


//Sparse Table
/*const int MAXN=200007;
const int LOG=18;
int min_st[MAXN][LOG];
int max_st[MAXN][LOG];
int log_table[MAXN + 7];

void buildSparseTables(int n,int arr[]) {
    log_table[1]=0;
    for (int i=2; i<=n;i++)
        log_table[i]=log_table[i / 2]+1;

    for (int i = 0;i<n;i++) {
        min_st[i][0]=arr[i];
        max_st[i][0]=arr[i];
    }

    for (int j = 1;(1 << j)<= n;j++)
        for (int i=0; i+(1 << j)<= n; i++) {
            min_st[i][j]=min(min_st[i][j - 1], min_st[i + (1 << (j - 1))][j - 1]);
            max_st[i][j]=max(max_st[i][j - 1], max_st[i + (1 << (j - 1))][j - 1]);
        }
}

int queryMin(int L, int R) {
    int j = log_table[R - L + 1];
    return min(min_st[L][j], min_st[R - (1 << j) + 1][j]);
}

int queryMax(int L, int R) {
    int j = log_table[R - L + 1];
    return max(max_st[L][j], max_st[R - (1 << j) + 1][j]);
}*/


/*const int N=200001;
map<int,int>id_col,mxber;
int n,m,k,col[N],mx=0,total=0;
vector<int>grp[N];
bool vis[N]= {0};

void dfs(int node) {
	vis[node]=1;
	total++;
	mxber[col[node]]++;
	mx=max(mx,mxber[col[node]]);

	for(auto child:grp[node]) {
		if(vis[child]) continue;
		dfs(child);
	}
	return;
}

const int N=1001;
ll fact[N];

void facto(){
	fact[1]=1;
	for(int i=2;i<=N;i++){
		fact[i]=fact[i-1]*i;
	}
}

ll binexp(ll a, ll b) {
    ll res = 1;
    while (b > 0) {
        if (b & 1)
            res = (res * a)%mod;
        a = (a * a)%mod;
        b >>= 1;
    }
    return res;
}

ll MMI(ll den){
	return binexp(den,mod-2);
}

ll nCr(ll n, ll r){
	ll den=MMI((fact[n-r]%mod*fact[r]%mod)%mod);
	return (fact[n]%mod*den)%mod;
}

ll lcm(ll a, ll b){
    return (a/__gcd(a,b))*b;
}*/

void solve(){
    ll n,m;
    cin>>n>>m;
    string s;
    cin>>s;

    vector<multiset<char> >tracer(26);
    int counter[26]={0};
    
    for(int i=0;i<n;i++)
        counter[s[i]-'a']++;

    vector<char>vec;
    for(int i=0;i<26;i++)
        if(counter[i]) vec.push_back(i+'a');
    
    string str;
    int start=0;
    for(int i=0;i<n;i++){
    	int in=vec[start]-'a';
        if(m>0) str+=vec[start];
        else{
        	if(counter[s[i]-'a']==0 || !tracer[s[i]-'a'].empty()) {
        		str+=*tracer[s[i]-'a'].begin();
        		tracer[s[i]-'a'].erase(tracer[s[i]-'a'].begin());
			}
			else{
				str+=s[i];
				counter[s[i]-'a']--;
			}
			continue;
		}
        
        if(s[i]==vec[start] || m==0){
        	counter[in]--;
        	if(counter[in]==0) start++;
            continue;
        }
        
        if(tracer[s[i]-'a'].find(vec[start])!=tracer[s[i]-'a'].end()){
            tracer[s[i]-'a'].erase(tracer[s[i]-'a'].find(vec[start]));
            counter[in]--;
            if(counter[in]==0) start++;
            continue;
        }
        
        bool flag=1;
        for(int j=0;j<26;j++){
            if(tracer[j].find(vec[start])!=tracer[j].end()){
                tracer[j].erase(tracer[j].find(vec[start]));
                counter[in]--;
                if(counter[in]==0) start++;
                tracer[j].insert(s[i]);
                m--;
                flag=0;
                break;
            }
        }
        
        if(!flag) continue;
        tracer[in].insert(s[i]);
        counter[in]--;
        if(counter[in]==0) start++;
        m--;
    }
    
    cout<<str<<endl;
}

int main() {
	FastIO;
	int n=1;
	cin>>n;
	while(n--) {
		solve();
	}
	return 0;
}
//written by MAMUN

/*  Cautions:
	1.array out of bound!!! [check array size and last accessed index]
	2.signed integer overflow!!! [int*int!=ll @typecast(ll) one of them or, multiply with 1LL]
	3.floating point numbers numbers are equal if the difference between them is less than 1e-9 [better way to compare floating point numbers]
	4.any square number when divided by 4 must have a remainder of either 0 or 1.
*/


/********************************************************************************************************************************************************
 _   _            _   _       _____          _     _   _       _                    _ _          ______                   _           _           _
| \ | |          | | | |     |  ___|        | |   | | | |     (_)                  (_) |         | ___ \                 | |         | |         | |
|  \| | ___  _ __| |_| |__   | |__  __ _ ___| |_  | | | |_ __  ___   _____ _ __ ___ _| |_ _   _  | |_/ / __ _ _ __   __ _| | __ _  __| | ___  ___| |__
| . ` |/ _ \| '__| __| '_ \  |  __|/ _` / __| __| | | | | '_ \| \ \ / / _ \ '__/ __| | __| | | | | ___ \/ _` | '_ \ / _` | |/ _` |/ _` |/ _ \/ __| '_ \
| |\  | (_) | |  | |_| | | | | |__| (_| \__ \ |_  | |_| | | | | |\ V /  __/ |  \__ \ | |_| |_| | | |_/ / (_| | | | | (_| | | (_| | (_| |  __/\__ \ | | |
\_| \_/\___/|_|   \__|_| |_| \____/\__,_|___/\__|  \___/|_| |_|_| \_/ \___|_|  |___/_|\__|\__, | \____/ \__,_|_| |_|\__, |_|\__,_|\__,_|\___||___/_| |_|
                                                                                           __/ |                     __/ |
                                                                                          |___/                     |___/
*********************************************************************************************************************************************************/

Information

Submit By
Type
Submission
Problem
P1058 Lexicographically Smallest String
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-31 17:28:35
Judged At
2024-11-11 02:34:21
Judged By
Score
5
Total Time
1ms
Peak Memory
324.0 KiB