Alphabetic Removals(模拟水题)
You are given a stringssconsisting ofnnlowercase Latin letters. Polycarp wants to remove exactlykkcharacters (k≤nk≤n) from the stringss. Polycarp uses the following algorithmkktimes:
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 exactlykktimes, thus removing exactlykkcharacters.
Help Polycarp find the resulting string.
Input
The first line of input contains two integersnnandkk(1≤k≤n≤4⋅1051≤k≤n≤4⋅105) — the length of the string and the number of letters Polycarp will remove.
The second line contains the stringssconsisting ofnnlowercase Latin letters.
Output
Print the string that will be obtained fromssafter Polycarp removes exactlykkletters using the above algorithmkktimes.
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
題目意思:給你一個(gè)含有n個(gè)字符的字符串,刪除其中的k個(gè)字符,按照字典序列刪除,即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;///是否刪除的狀態(tài)
9 char ch;///字符
10 } s[400010];
11 char x[400010];
12 int my_comp1(node a,node b)///先按照字典序排序升序,字典序相同時(shí)按照在字符串位置升序
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("
");
57 return 0;
58 }
方法二:成批次地按照字典序刪除字符,直到刪夠k個(gè)結(jié)束,時(shí)間復(fù)雜度會(huì)更低
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 }
總結(jié)
以上是生活随笔為你收集整理的Alphabetic Removals(模拟水题)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于MyEclipse项目的名字的修改对
- 下一篇: 烤盐屋——凡事预则立