CodeForces - 999 CAlphabetic Removals

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值