Alphabetic Removals(模拟水题)

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 kkletters 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

 

题目意思:给你一个含有n个字符的字符串,删除其中的k个字符,按照字典序列删除,即abcdef......,求出删除完成之后的字符串。

 

方法一:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 struct node
 6 {
 7     int id;///字符在字符串中的位置
 8     int vis;///是否删除的状态
 9     char ch;///字符
10 } s[400010];
11 char x[400010];
12 int my_comp1(node a,node b)///先按照字典序排序升序,字典序相同时按照在字符串位置升序
13 {
14     if(a.ch==b.ch)
15     {
16         return a.id<b.id;
17     }
18     else
19     {
20         return a.ch-'a'<b.ch-'a';
21     }
22 }
23 int my_comp2(node a,node b)///再按照在字符串中的位置升序
24 {
25     return a.id<b.id;
26 }
27 int main()
28 {
29     int n,k,i,len;
30     scanf("%d%d",&n,&k);
31     scanf("%s",x);
32     len=strlen(x);
33     for(i=0; i<len; i++)
34     {
35         s[i].ch=x[i];
36         s[i].id=i;
37         s[i].vis=0;
38     }
39     sort(s,s+len,my_comp1);
40     for(i=0; i<k; i++)
41     {
42         s[i].vis=1;
43     }
44     sort(s,s+len,my_comp2);
45     for(i=0; i<len; i++)
46     {
47         if(s[i].vis==1)
48         {
49             continue;
50         }
51         else
52         {
53             printf("%c",s[i].ch);
54         }
55     }
56     printf("\n");
57     return 0;
58 }

 

方法二:成批次地按照字典序删除字符,直到删够k个结束,时间复杂度会更低

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 char s[400010];
 6 char a[]="abcdefghijklmnopqrstuvwxyz";
 7 int main()
 8 {
 9     int n,k;
10     int i,j;
11     char ch;
12     while(scanf("%d%d",&n,&k)!=EOF)
13     {
14        getchar();
15        scanf("%s",s);
16        j=0;
17        ch=a[j];
18        while(1)
19         {
20             for(i=0; i<n; i++)
21             {
22                 if(s[i]==ch)
23                 {
24                    s[i]=' ';
25                    k--;
26                 }
27                 if(k==0)
28                 {
29                     break;
30                 }
31             }
32             ch=a[++j];
33             if(k==0)
34             {
35                 break;
36             }
37         }
38         for(i=0;i<n;i++)
39         {
40             if(s[i]!=' ')
41             {
42                 printf("%c",s[i]);
43             }
44         }
45     }
46     return 0;
47 }

 

 

 

posted @ 2018-07-20 15:54  王陸  阅读(248)  评论(1编辑  收藏  举报