CodeForces (字符串从字母a开始删除k个字母)

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 1

15 3
cccaabababaccbc

Output 1

cccbbabaccbc

Input 2

15 9
cccaabababaccbc

Output 2

cccccc

Input 3

1 1
u

Output 3

思路:

类比桶排序,用桶来存字母的个数

代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <math.h>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 const int maxn=4*1e5+10;
17 using namespace std;
18
19 char str[maxn];
20 int cnt[27];//原来字母的个数
21 int num[27];//处理后字母的个数
22
23 int main()
24 {
25     int n,k;
26     scanf("%d %d",&n,&k);
27     getchar();
28     for(int i=0;i<n;i++)
29     {
30         scanf("%c",&str[i]);
31         cnt[str[i]-‘a‘+1]++;
32         num[str[i]-‘a‘+1]++;
33     }
34     str[n]=0;
35     for(int i=1;i<=26;i++)//从‘a‘开始进行k次处理
36     {
37         while(num[i]&&k)
38         {
39             num[i]--;
40             k--;
41         }
42     }
43     for(int i=0;str[i];i++)
44     {
45         if(cnt[str[i]-‘a‘+1]>num[str[i]-‘a‘+1])//说明该位置字母已删除
46         {
47             cnt[str[i]-‘a‘+1]--;
48             continue;
49         }
50         else //若该位置字母没被删,输出该字母
51             printf("%c",str[i]);
52     }
53     return 0;
54 }

原文地址:https://www.cnblogs.com/jiamian/p/11620309.html

时间: 2024-07-31 03:46:20

CodeForces (字符串从字母a开始删除k个字母)的相关文章

(笔试题)删除K位数字

题目: 现有一个 n 位数,你需要删除其中的 k 位,请问如何删除才能使得剩下的数最大? 比如当数为 2319274, k=1 时,删去 2 变成 319274 后是可能的最大值. 思路: 1.贪心算法 每次从高位向低位数,删除高位数字比低位数字小的那位数字.如2319274 第一次2<3,删除2,得到319274 第二次3>1,略过,1<9,删除1,得到39274 第三次3<9,删除3,得到9274 ...... // greedy method string deleteKBi

C#根据用户输入字符串,输出大写字母有几个,小写字母有几个

static void Main(string[] args) { // 根据用户输入字符串,输出大写字母有几个,小写字母有几个. Console.WriteLine("请输入一行英文代码"); string s = Console.ReadLine(); //用一个字符串接受输入值. int i = 0; int j = 0;// i是大写个数, j是小写个数. foreach (char s1 in s) { if (s1 >= 'A' && s1 <=

将字符串中的三个单词的首字母转化成大写

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script> //var a = 'welcome to china'; //将字符串中的三个单词的首字母转化成大写:返回Welcome To China; var a = 'welcome to china'; va

删除K个数字后的最小值(贪心算法实现)

给出一个整数,从该整数中去掉k个数字,要求剩下的数字形成的新整数尽可能小.应该如何选取被去掉的数字? 其中,整数的长度大于或等于k,给出的整数的大小可以超过long类型的数字范围. 思路: 把原整数的所有数字从左到右进行比较,如果发现某一位数字大于它右面的数字,那么在删除该数字之后,必然会使该数位的值降低.这种求局部最优解,最终得到全局最优解的思想,叫作“贪心算法”. 如果要删除k个数字,那么将遍历数字作为外层循环,以k作为外层循环,再结合栈的使用.将遍历的数字逐个入栈,遇到入栈进来的数字小于栈

JS获取中文拼音首字母,并通过拼音首字母快速查找页面内的中文内容

实现效果: 图一: 图二: 此例中输入的中文字符串"万万保重",有三个字是多音字,所以alert对话框中显示的是多种读音的组合: 如何实现? 如何实现通过拼音首字母快速查找页面内的中文内容呢? 过程原理是这样的:例如要对一些人名进行快速查找,当页面加载完成后,对所有人名建立一个索引,生成拼音首字母与姓名的对应关系:然后监听键盘事件,当用户按下键盘时,根据键值得到按下的是哪个字母,然后遍历索引中是否存在相同的拼音首字母: 这里还实现了根据字母组合来查找的功能,原理是这样的:当用户按键时,

3、从键盘上接收一个字母,判断是否是大写字母,如果是则转换成小写字母输出

3.从键盘上接收一个字母,判断是否是大写字母,如果是则转换成小写字母输出,否则直接输出 #include <stdio.h>void main(){   char x=0;    printf("请输入一个字母:");    scanf("%c",&x);    x>=65&&x<=90? printf("小写字母为:%c",x+32): printf("%c",x);    p

工具类:获得随机字母和数字的组合(字母+数字组合,字母组合,数字组合)

package util; import java.util.Random; /** * * @author jkfeng * 获得随机字母和数字的组合(字母+数字组合,字母组合,数字组合) * */ public class RandomCharOrNumUtil { public static void main(String[] args) { System.out.println(getCharAndNum(6)); System.out.println(getChar(6)); Sys

查询表数据的时让表内数据全大写、小写、首字母大写、前几位字母大写

select                       Upper(Substring(列名, 1, 1)) + Lower(Substring(列名, 2,LEN(列名))) AS 想显示的列名,        UPPER(列名) AS 想显示的列名,                   Upper(Substring(列名, 1, 6)) + Substring(列名, 7,LEN(列名)) AS 想显示的列名,       LOWER(列名)AS 想显示的列名,             

定义一个类:实现功能可以返回随机的10个数字,随机的10个字母, 随机的10个字母和数字的组合;字母和数字的范围可以指定,类似(1~100)(A~z)

#习题2:定义一个类:实现功能可以返回随机的10个数字,随机的10个字母, #随机的10个字母和数字的组合:字母和数字的范围可以指定 class RandomString(): #随机数选择的范围作为参数,如(1~100)字母 ('A'~'z'),大写字母在前 按ascii值排列先后 def __init__(self,start_num=0,end_num=100,start_alpha='A',end_alpha='z'): import string if not isinstance(s