Codeforces Round #490 (Div. 3).C. Alphabetic Removals(思维题+容器排序)

本文介绍了一道关于从字符串中按字母顺序移除特定数量字符的算法题目,并提供了三种不同的实现方法,包括使用向量排序、数组标记及直接修改等技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Alphabetic Removals
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove exactly kk characters (knk≤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 (1kn41051≤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
Copy
15 3
cccaabababaccbc
output
Copy
cccbbabaccbc
input
Copy
15 9
cccaabababaccbc
output
Copy
cccccc
input
Copy
1 1
u
output
考点:思维题

方法一:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    vector<pair<char,int> > v;
    int n,k;
    string x;
    cin>>n>>k>>x;
    for(int i=0; i<n; i++)
        v.push_back(make_pair(x[i],i));
    sort(v.begin(),v.end());ikui
    for(int i=0; i<k; i++)
        x[v[i].second]='*';
    for(int i=0; i<n; i++)
        if(x[i]!='*') cout<<x[i];
}

方法二:

#include <bits/stdc++.h>
using namespace std;
int n, k, f[400050], id;
string s;
int main()
{
	cin >> n >> k >> s;
	for(int j = 0; j < 26; j++)
		for(int i = 0; i < n; i++)
		    if(s[i] - 'a' == j)
                f[i] = ++id;
	for(int i = 0; i < n; i++)
        if(f[i] > k)
            cout << s[i];
}

方法三:

#include <bits/stdc++.h>
#define ll long long
#define N 1000005
using namespace std;

int n,k;
char s[N];

int main(){
	scanf("%d%d%s",&n,&k,s+1);
	for(char c='a';k>0&&c<='z';++c)
		for(int i=1;k>0&&i<=n;++i)
			if(s[i]==c)
				--k,s[i]=0;
	for(int i=1;i<=n;++i)
		if(s[i])putchar(s[i]);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值