code forces 999C Alphabetic Removals
2 seconds
256 megabytes
standard input
standard output
You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove exactly kk characters (k≤nk≤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.
The first line of input contains two integers nn and kk (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 string ss consisting of nn lowercase Latin letters.
Print the string that will be obtained from ss after Polycarp removes exactly kk letters 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).
15 3
cccaabababaccbc
cccbbabaccbc
15 9
cccaabababaccbc
cccccc
1 1
u
#include<bits/stdc++.h>
using namespace std;
int n;
string str;
int k;
vector<int> cnt();
int main() { cin>>n>>k;
cin>>str; for(int i=; i<n; i++) {
cnt[str[i]-'a']++;
}
//记录下每个字母的数量
//从a开始减去存在的字母
//如果没有了 就记录下还可以输出的字母
int pos=;
for(int i=; i<; i++) {
if(k>=cnt[i]) {
k-=cnt[i];
} else {
pos=i;
break;
}
} string ans;
int rem=k;
for(int i=; i<n; i++) {
int cur=str[i]-'a';
//在pos后面的字母可以用
//用完了k的字母可以用 if(cur>pos||(cur==pos&&rem==)) {
ans+=str[i];
} else if(cur==pos) {
rem--;
}
}
cout<<ans<<endl; return ;
}
code forces 999C Alphabetic Removals的更多相关文章
- CodeForces - 999C Alphabetic Removals
C - Alphabetic Removals ≤k≤n≤4⋅105) - the length of the string and the number of letters Polycarp wi ...
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- CF999C Alphabetic Removals 思维 第六道 水题
Alphabetic Removals time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Code Forces 796C Bank Hacking(贪心)
Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...
- Code Forces 833 A The Meaningless Game(思维,数学)
Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...
- Code Forces 543A Writing Code
题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...
- CF999C Alphabetic Removals 题解
Content 给定一个长度为 \(n\) 的仅含小写字母的字符串,执行 \(k\) 次如下操作: 如果字符串中有 a 这个字母,删除从左往右第一个 a,并结束操作,否则继续操作: 如果字符串中有 b ...
- code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)
Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...
- code forces 382 D Taxes(数论--哥德巴赫猜想)
Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
随机推荐
- Shell 入门笔记(一)
Shell简介 在开发过程中Linux系统经常接触和使用的,Shell 是我们用户使用 Linux 的桥梁,是C 语言编写的程序.Shell 是一种命令语言,同时一种程序设计语言.对大多数开发人员来说 ...
- Centos7 使用LVM进行新加磁盘管理
centos7使用LVM管理一块新的磁盘 注意!文中凡是带#的都是命令标志. 一些重要概念: LV(Logical Volume)- 逻辑卷, VG(Volumne Group)- 卷组, P ...
- 读键值对封装成Map
描述: 有配置文件address_relation.properties,记录地址关系,有如下数据:ZSSS=ZS%,ZSPD, 封装到Map代码如下: public static void main ...
- 两种js方法发起微信支付:WeixinJSBridge,wx.chooseWXPay区别
原文链接:https://www.2cto.com/weixin/201507/412752.html 1.为什么会有两种JS方法可以发起微信支付? 当你登陆微信公众号之后,左边有两个菜单栏,一个是微 ...
- php扩展开发-常量
//常量在内核中的结构 typedef struct _zend_constant { zval value; int flags; char *name; uint name_len; int mo ...
- 关于IT术语---ip、uv、pv、tps、qps、rps
涉及到IT方面的几条术语,这里要好好说道说道: 只要和网站打交道,难免会经常听到一系列的转有名词 >>> 系统今日UV多少.PV多少.QPS多少之类的问题.这里就对这些常见的术语 ...
- 将Komodo Edit打造成Python开发的IDE
Komodo Edit 支持Python 界面清爽, 将Komodo Edit 设置成Python的IDE,具体操作方法如下: 先添加自定义命令. 再设置命令行参数 设置高级选项 设置快捷键 完成.
- java练习——多态与异常处理
1. 上面的程序运行结果是什么? 2. 你如何解释会得到这样的输出? parent = chlid; 所以child中的方法被赋予parent,所以用child方法输出了child的成员变量: ...
- ARC下还会存在内存泄露吗?
1.第三方框架不正当使用.2.block,delegate,NSTimer循环使用.3.非oc对象的内存处理.4.地图类处理.5.大次数循环内存暴涨. 非oc对象的释放: 例如使用CGImageRel ...
- css3实现圆角边框渐变
<button class="border">112233</button> 创建button .border{ position: relative; b ...