B - train 2(2019.10 NCWU第一次训练)(字符串删除)

题目: You are given a string s consisting of n lowercase Latin letters. Polycarp

题目:

You are given a string s consisting of n lowercase Latin letters. Polycarp wants to remove exactly k characters (k≤n) from the string s. Polycarp uses the following algorithm k 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 k times, thus removing exactly k characters.

Help Polycarp find the resulting string.

Input
The first line of input contains two integers n and k (1≤k≤n≤4⋅105) — the length of the string and the number of letters Polycarp will remove.

The second line contains the string s consisting of n lowercase Latin letters.

Output
Print the string that will be obtained from s after Polycarp removes exactly k letters using the above algorithm k 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
(注意,这里没有输出)

题目大意:

给你一个指定长度的均由小写字母组成的字符串,然后从中删除指定个数的字符,从a开始删,a删完了删b……一直删到符合要求为止。

刚开始理解错题意了,以为删z的时候只删第一个就可以了,没想到也是删到符合要求为止。注意,题目给的意思就是只要a>=1就删,即把a删完才继续删除b,而不是剩一个a不删!

AC代码:(略长,因为26个字母我都列出来了,各位大佬有更好的方法的话欢迎评论)

#include
#include
#include
using namespace std;
int main()
{int n=0,k=0;cin>>n>>k;char a[500000];scanf("%s",&a);for(int i=0;i<n;i++){if(a[i]=='a'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='b'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='c'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='d'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='e'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='f'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='g'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='h'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='i'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='j'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='k'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='l'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='m'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='n'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='o'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='p'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='q'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='r'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='s'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='t'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='u'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='v'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='w'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='x'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='y'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]=='z'&&k>0){a[i]='*';k--;}}for(int i=0;i<n;i++){if(a[i]!='*')printf("%c",a[i]);}return 0;
}