You are given a string 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 characters.
Help Polycarp find the resulting string.
The first line of input contains two integers 1≤k≤n≤4⋅105) — the length of the string and the number of letters Polycarp will remove.
The second line contains the string n lowercase Latin letters.
Print the string that will be obtained from k times.
If the resulting string is empty, print nothing. It is allowed to print nothing or an empty line (line break).
15 3 cccaabababaccbc
cccbbabaccbc
15 9 cccaabababaccbc
cccccc
1 1 u
AC代码为:
#include<bits/stdc++.h>
using namespace std;
const int maxn=4e5+10;
struct Node{
int id,temp;
char c;
} node[maxn];
bool cmp1(Node a,Node b)
{
return a.c==b.c? a.id<b.id : a.c-'a'<b.c-'a';
}
bool cmp2(Node a,Node b)
{
return a.id<b.id;
}
int n,k;
char s1[maxn];
int main()
{
scanf("%d%d",&n,&k);
scanf("%s",s1);
int len=strlen(s1);
for(int i=0;i<len;i++)
{
node[i].c=s1[i];
node[i].id=i;
node[i].temp=0;
}
sort(node,node+len,cmp1);
for(int i=0;i<k;i++) node[i].temp=1;
sort(node,node+len,cmp2);
for(int i=0;i<len;i++)
{
if(node[i].temp==1) continue;
else printf("%c",node[i].c);
}
printf("\n");
return 0;
}