CodeForces - 999C Alphabetic Removals
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
15 3
cccaabababaccbc
cccbbabaccbc
15 9
cccaabababaccbc
cccccc
1 1
u
这个题就是给你一个已知长度的字符串,给你一个数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的更多相关文章
- code forces 999C Alphabetic Removals
C. Alphabetic Removals time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- CF999C Alphabetic Removals 思维 第六道 水题
Alphabetic Removals time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- C - Alphabetic Removals
题目链接: You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove e ...
- CodeForces - 999C
You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove exactly ...
- Alphabetic Removals(模拟水题)
You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove exactly ...
- Codeforces 1237C2. Balanced Removals (Harder)
传送门 先来考虑一下二维时的情况,那么对于 $x$ 相同的点,我们按 $y$ 排序,然后相邻的一对对消除 最后 $x$ 坐标相同的点最多剩下一个,那么此时所有点的 $x$ 坐标都不一样 再按 $x$ ...
- CF999C Alphabetic Removals 题解
Content 给定一个长度为 \(n\) 的仅含小写字母的字符串,执行 \(k\) 次如下操作: 如果字符串中有 a 这个字母,删除从左往右第一个 a,并结束操作,否则继续操作: 如果字符串中有 b ...
- Codeforces Round #490 (Div. 3)
感觉现在\(div3\)的题目也不错啊? 或许是我变辣鸡了吧....... 代码戳这里 A. Mishka and Contes 从两边去掉所有\(≤k\)的数,统计剩余个数即可 B. Reversi ...
- [Codeforces]Codeforces Round #490 (Div. 3)
Mishka and Contest #pragma comment(linker, "/STACK:102400000,102400000") #ifndef ONLINE_JU ...
随机推荐
- PHP运行机制和原理
以echo "Hello World";为例 经历五个步骤:1.扫描(scanning):先进行语法分析和词法分析,然后将index.php内容变成一个个语言片段(token ar ...
- RestControllerAdvice,ControllerAdvice
1.切记@RestControllerAdvice 和 @ControllerAdvice 不能放在common里,会不生效,还会引起子项目的全局异常失败. 所以这2个还是放在各自的子项目里去处理.一 ...
- SVN 问题解决之 The XML response contains invalid XML
公司几个同事的SVN更新时出现了The XML response contains invalid XML报错 经Google得到一个线索,可能和Http请求有关. 想起之前项目改过一次网络请求方式, ...
- 从linux和ucos的比较中来看进程这个概念
这种问题就要和ucos结合起来嘛. 程序和进程: 程序:存放在磁盘上的一些列代码和数据的可执行映像,是一个静止的实体. 进程:是一个执行中的程序,它是动态的实体. linux进程的四要素: 1. 有一 ...
- 将mysql从5.5.25升级到8.0.12
将mysql从5.5.25升级到8.0.12过程中遇到几个问题,记录如下: 将数据库安装好后,导入原来的数据,启动tomcat,报错unable to load authentication ca ...
- 06 Windows编程——设备句柄 和 WM_PAINT消息
windows程序在现实方式上属于图形方式,和文字方式的显示,有显著的不同. 什么是设备句柄,如何获取 使用统一的数据结构表示某一设备,这个结构就是设备句柄. 源码 #include<Windo ...
- 12.自定义v-过渡动画前缀
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- Python For嵌套循环 图形打印X型 nested loop - 练习题答案
上一篇:Python For嵌套循环 图形打印X型 nested loop - 练习题 上一篇留的Python For嵌套循环 图形打印X型练习题的答案. 由于网上很多嵌套循环都是C++语言写的,用P ...
- PAT Basic 1069 微博转发抽奖 (20 分)
小明 PAT 考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔 N 个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数 M(≤ 1000). ...
- RedHat Enterprise Linux 5 配置Samba服务器
1.修改samba的配置文件 # gedit /etc/samba/smb.conf 在/etc/samba/smb.conf配置文件中找到Share Definitions模块添加以下代码: [ro ...