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 ...
随机推荐
- 【解决方法】You seem to have the current working directory in your LD_LIBRARY_PATH environment variable.
参考地址:https://blog.csdn.net/qq_24755999/article/details/78722788 You seem to have the current working ...
- C# 对象互转
/// <summary> /// 适用于初始化新实体 /// </summary> static public T RotationMapping<T, S>(S ...
- CAS 5.x搭建常见问题系列(3).Failure to find org.apereo.cas:cas-server-support-pm-jdbc:jar:5.1.9
错误内容 cas overlay的pom.xml增加了cas-server-support-pm-jdbc.jary依赖后, 打包(mvn package)出现如下的报错 D:\casoverlay\ ...
- (二)shiro之jsp标签
一.介绍 <%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %> Guest ...
- 在CentOS部署AspNetCore网站
前段时间某云服务器大促,就买了一台打算折腾一下,买了几个月,却啥也没做,就改了个初始密码.最近快到双十一了,另一家厂商相同配置的服务器价格又便宜了一大截,看来又得剁手了.从今年开始,搜索一下云服务器, ...
- vue引入警告:There are multiple modules with names that only differ in casing. This can lead to unexpected behavior when compiling on a filesystem with other case-semantic. Use equal casing. Compare these
在写vue项目的时候 当我使用 : import dataSource from '../overseaProduct/house/dataSource'; 引入dataSource文件的时候:控制台 ...
- 编译openwrt backfire过程中出现的问题
参考的步骤如链接: http://www.right.com.cn/forum/forum.php?mod=viewthread&tid=124604 在make menuconfig的时候出 ...
- 使用Java Executor框架实现多线程
本文将涵盖两个主题: 通过实现Callable接口创建线程 在Java中使用Executor框架 实现Callable接口 为了创建一段可以在线程中运行的代码,我们创建了一个类,然后实现了Callab ...
- MVC,MVP 和 MVVM 的图示(原理)
一.MVC MVC模式的意思是,软件可以分成三个部分. 视图(View):用户界面. 控制器(Controller):业务逻辑 模型(Model):数据保存 各部分之间的通信方式如下. View 传送 ...
- 记录在苹果X手机上运行遇到的代码Dom被阻塞不更新的一个坑
一.问题产生背景: 开发支付功能,代码逻辑如下:点击支付后,请求后台接口得到流水号以及第三方支付台链接,跳转支付台(在苹果手机则是弹出支付台层):支付完毕后返回支付页面,或中途退出支付台返回支付页面: ...