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

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


猜你喜欢

转载自blog.csdn.net/xxxxxm1/article/details/80768344