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 ...
随机推荐
- nodejs 发送邮件(阿里云)
1.下载 模块 2. 编辑文件 var nodemailer = require('nodemailer'); var sendEmail = function(emailinfo,callbac ...
- centos7 多网卡修改默认路由
最近在virtualbox里搭了一个centos7的虚拟机,但是网络这一块总是有问题. 单网卡下的问题: 1.当我配置连接方式为NAT网络地址转换的时候,虚拟机可以访问外网.但是在网络地址转换的情况下 ...
- redis 面试题
https://www.cnblogs.com/ftl1012/p/redisExam.html 1. 使用Redis有哪些好处? (1) 速度快,因为数据存在内存中,类似于HashMap,HashM ...
- 使用windows api安装windows服务程序(C#)
3个步骤: 1.安装器代码编写 2.安装器工具类编写 1)安装.启动服务) 2)卸载服务 3.windows服务程序编写(参考:多线程.方便扩展的Windows服务程序框架) 4.代码下载,在文末(注 ...
- PHP常用的自定义函数
PHP常用的自定义函数 目录 php常用自定义函数类下载 php 设置字符编码为utf-8 路径格式化(替换双斜线为单斜线) 转码 打印输出 api返回信息 字符串截取 方法一: 方法二: 数组 字符 ...
- RabbitMQ实现中AMQP与MQTT消息收发异同
实现了AMQP与MQTT(至多一次)后,用多个队列以topic exchange的方式用相同交换机监听同一个主题(topic),发现情况存在不同,觉得有点意思,所以记录了下来. 用2个MQTT(分别记 ...
- 笔记-python-语法-super
笔记-python-语法-super 1. super 1.1. super起源 首先要理解类的继承和多继承,子类有时需要调用父类的方法, 非绑定方法: class C(B): def ...
- Fragment Touch事件泄露
当Fragment的栈里面有几个fragment的时候,这个时候如果是几个fragment状态是hide,当你触摸当前fragment的时候,下层的fragment的事件被触发,这是由于Touch事件 ...
- tools.jar seem to ....
android stadio 运行不起来,可以在java_home原来的路径下加一个\, 然后就可以运行起来了.
- springboot遇见问题总结
今天开始学习创建springboot项目 问题1: 产生异常: 创建项目目录: demo代码: 代码Controller import org.springframework.web.bind.ann ...