CodeForces - 999 CAlphabetic Removals
You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove exactly kk characters (k≤nk≤n) from the string ss. Polycarp uses the following algorithm kk times:
if there is at least one letter ‘a’, remove the leftmost occurrence and stop the algorithm, otherwise go to next item;
if there is at least one letter ‘b’, remove the leftmost occurrence and stop the algorithm, otherwise go to next item;
…
remove the leftmost occurrence of the letter ‘z’ and stop the algorithm.
This algorithm removes a single letter from the string. Polycarp performs this algorithm exactly kk times, thus removing exactly kk characters.
Help Polycarp find the resulting string.
Input
The first line of input contains two integers nn and kk (1≤k≤n≤4⋅1051≤k≤n≤4⋅105) — the length of the string and the number of letters Polycarp will remove.
The second line contains the string ss consisting of nn lowercase Latin letters.
Output
Print the string that will be obtained from ss after Polycarp removes exactly kk letters using the above algorithm kk times.
If the resulting string is empty, print nothing. It is allowed to print nothing or an empty line (line break).
Examples
Input
15 3
cccaabababaccbc
Output
cccbbabaccbc
Input
15 9
cccaabababaccbc
Output
cccccc
Input
1 1
u
Output
因为没有给t赋初值WA了两发,这真是一个悲伤的故事,因为电脑卡在结束后一分钟交上了,这是一个更悲伤的故事。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<set>
#define N 200100
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int INF=99999999;
const int MAX=2e6+5;
char a[550000];
int c[28];
int main(int argc, char** argv) {
memset(c,0,sizeof(c));
int n,k,i,t,j,sum;
sum=0;
t=25;
scanf("%d%d",&n,&k);
getchar();
for(i=0;i<n;i++){
scanf("%c",&a[i]);
c[a[i]-'a']++;
}
for(i=0;i<26;i++){
sum+=c[i];
if(sum>k){
t=i-1;
if(sum-c[i]<0){
sum=0;
}
else{
sum-=c[i];
}
break;
}
}
// cout<<t<<endl;
// cout<<sum<<endl;
for(i=0;i<n;i++){
if(a[i]-'a'<=t){
continue;
}
if(a[i]-'a'==t+1&&sum<k){
sum++;
// cout<<i<<" "<<sum<<endl;
continue;
}
printf("%c",a[i]);
}
return 0;
}