C - Alphabetic Removals

You are given a string

s consisting of n lowercase Latin letters. Polycarp wants to remove exactly k characters (k≤n≤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 kkletters 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

这个题就是给你一个已知长度的字符串,给你一个数k,要求要从这里面删去k个字符,删去的这k个字符要求是从a开始删除,如果a没有删完,那么就不能删除b,依次进行,直到删完k个字符就可以了。

#include <bits/stdc++.h>

using namespace std;

char s[400005];
int a[1000]; // a用来标记每个字母出现的次数
int main()
{
int n, k;
while(~scanf("%d %d",&n, &k))
{
getchar();
memset(a, 0,sizeof(a));
for(int i = 0; i < n; i ++)
{
scanf("%c",&s[i]);
a[s[i]] ++;
}
if(k >= n) printf("\n"); //如果需要删除的k大于n,就全部删除没了
else
{
int num = 0;
for(int i = 97; ; i ++) // 从a开始删除,如果满足k>=a[i],把所有的字符都删去即可
{
if(k >= a[i])
{
num ++;
k = k - a[i];
}
else break;
}
for(int i = 0; i < n; i ++)
{ if(s[i] < 97 + num); //删除掉的不再输出
else
{
if(s[i] == 97 + num) // 没有全部删除完成,继续删除这个字符
{
if(k > 0)k--;
else printf("%c",s[i]);
}
else printf("%c",s[i]); // 输出不需要删除的
} }
printf("\n");
}
}
return 0;
}
发现可以省去好多代码:
#include <bits/stdc++.h>
using namespace std;
int n,m;
char str[400005]; int main()
{
cin>>n>>m;
cin>>str;
for(int i=0;i<=25;i++){
for(int j=0;j<n&&m;j++){
if(str[j] == 'a' + i){
str[j] = '0';
m--;
}
}
}
for(int i=0;i<n;i++){
if(str[i] != '0')cout<<str[i];
}
return 0;
}

CodeForces - 999C Alphabetic Removals的更多相关文章

  1. code forces 999C Alphabetic Removals

    C. Alphabetic Removals time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  2. CF999C Alphabetic Removals 思维 第六道 水题

    Alphabetic Removals time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. C - Alphabetic Removals

    题目链接: You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove e ...

  4. CodeForces - 999C

    You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove exactly ...

  5. Alphabetic Removals(模拟水题)

    You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove exactly ...

  6. Codeforces 1237C2. Balanced Removals (Harder)

    传送门 先来考虑一下二维时的情况,那么对于 $x$ 相同的点,我们按 $y$ 排序,然后相邻的一对对消除 最后 $x$ 坐标相同的点最多剩下一个,那么此时所有点的 $x$ 坐标都不一样 再按 $x$ ...

  7. CF999C Alphabetic Removals 题解

    Content 给定一个长度为 \(n\) 的仅含小写字母的字符串,执行 \(k\) 次如下操作: 如果字符串中有 a 这个字母,删除从左往右第一个 a,并结束操作,否则继续操作: 如果字符串中有 b ...

  8. Codeforces Round #490 (Div. 3)

    感觉现在\(div3\)的题目也不错啊? 或许是我变辣鸡了吧....... 代码戳这里 A. Mishka and Contes 从两边去掉所有\(≤k\)的数,统计剩余个数即可 B. Reversi ...

  9. [Codeforces]Codeforces Round #490 (Div. 3)

    Mishka and Contest #pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JU ...

随机推荐

  1. excelize

    // 参考:https://gitee.com/xurime/excelize // github.com/360EntSecGroup-Skylar/excelize

  2. (五)Activiti之查看最新版本的流程定义

    一.查看最新版本的流程定义 因为每个流程定义都可能会有好几个版本,所以有时候我们有这样的需求,查询出最新版本的流程定义的集合 第一步:我们通过Activiti接口来获取根据流程定义Version升序排 ...

  3. WebSocket协议探究(序章)

    一 WebSocket协议基于HTTP和TCP协议 与往常一样,进入WebSocket协议学习之前,先进行WebSocket协议抓包,来一个第一印象. WebSocket能实现客户端和服务器间双向.基 ...

  4. C# XML 序列化帮助类

    /// <summary> /// Xml helper class /// </summary> public static class XmlHelper { #regio ...

  5. Echarts配置项详解

    1.图表标题 title: { x: 'left', // 水平安放位置,默认为左对齐,可选为: // 'center' ¦ 'left' ¦ 'right' // ¦ {number}(x坐标,单位 ...

  6. iOS 9.0中UIAlertController的用法。

    1.我为什么要写这篇博客记录它? 答:因为 UIAlertView和UIActionSheet 被划线了 苹果不推荐我们使用这两个类了,也不再进行维护和更新,为了以后方便使用我来记录一下.如图所示 正 ...

  7. kNN(K-Nearest Neighbor)最邻近规则分类(转)

    KNN最邻近规则,主要应用领域是对未知事物的识别,即判断未知事物属于哪一类,判断思想是,基于欧几里得定理,判断未知事物的特征和哪一类已知事物的的特征最接近: K最近邻(k-Nearest Neighb ...

  8. 后台向前台响应的json数据格式的一些问题

    最近在写后台向前台easyUI页面发送数据时遇到的一些报错. 首先easyUI内部封装了许多的方法和对象,以至于很多参数都不清楚,需要查询,其次easyUI也是有内置ajax所以从后台响应回来的数据一 ...

  9. Computer Vision_18_Image Stitching:A survey on image mosaicing techniques——2013

    此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...

  10. mariadb-server安装问题(Error: MariaDB-common conflicts with 1:mariadb-libs-5.5.60-1.el7_5.x86_64)

    问题:今天在安装mariadb-server包时,提示错误,无法正确安装linux自带的mariadb包,提示错误很明确,是由于MariaDB-common包与mariadb-libs包冲突. 解决办 ...