CodeForces (字符串从字母a开始删除k个字母)
You are given a string s consisting of n lowercase Latin letters. Polycarp wants to remove exactly k characters (k≤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 k letters 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 1
cccaabababaccbc
Output 1
cccbbabaccbc
Input 2
cccaabababaccbc
Output 2
cccccc
Input 3
u
Output 3
思路:
类比桶排序,用桶来存字母的个数
代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const int maxn=*1e5+;
using namespace std; char str[maxn];
int cnt[];//原来字母的个数
int num[];//处理后字母的个数 int main()
{
int n,k;
scanf("%d %d",&n,&k);
getchar();
for(int i=;i<n;i++)
{
scanf("%c",&str[i]);
cnt[str[i]-'a'+]++;
num[str[i]-'a'+]++;
}
str[n]=;
for(int i=;i<=;i++)//从'a'开始进行k次处理
{
while(num[i]&&k)
{
num[i]--;
k--;
}
}
for(int i=;str[i];i++)
{
if(cnt[str[i]-'a'+]>num[str[i]-'a'+])//说明该位置字母已删除
{
cnt[str[i]-'a'+]--;
continue;
}
else //若该位置字母没被删,输出该字母
printf("%c",str[i]);
}
return ;
}
CodeForces (字符串从字母a开始删除k个字母)的更多相关文章
- codeforces 1038a(找最长的前k个字母出现相同次数的字符串)
codeforces 1038a You are given a string s of length n, which consists only of the first k letters of ...
- C语言:将ss所指字符串中所有下标为奇数位上的字母转换成大写,若不是字母,则不转换。-删除指针p所指字符串中的所有空白字符(包括制表符,回车符,换行符)-在带头结点的单向链表中,查找数据域中值为ch的结点,找到后通过函数值返回该结点在链表中所处的顺序号,
//将ss所指字符串中所有下标为奇数位上的字母转换成大写,若不是字母,则不转换. #include <stdio.h> #include <string.h> void fun ...
- C语言:将ss所指字符串中所有下标为奇数位置的字母转换为大写-将该字符串中的所有字符按ASCII码值升序排序后输出。-将a所指的4*3矩阵第k行的元素与第0行元素交换。
//函数fun:将ss所指字符串中所有下标为奇数位置的字母转换为大写,若不是字母,则不转换. #include<conio.h> #include<stdio.h> #incl ...
- Day_11【集合】扩展案例2_使用普通for循环获取集合中索引为3的元素并打印,统计集合中包含字符串"def"的数量,删除集合中的所有字符串",将集合中每个元素中的小写字母变成大写字母def",
分析以下需求,并用代码实现 1.定义ArrayList集合,存入多个字符串"abc" "def" "efg" "def" ...
- (笔试题)删除K位数字
题目: 现有一个 n 位数,你需要删除其中的 k 位,请问如何删除才能使得剩下的数最大? 比如当数为 2319274, k=1 时,删去 2 变成 319274 后是可能的最大值. 思路: 1.贪心算 ...
- Java-Runoob-高级教程-实例-字符串:03. Java 实例 - 删除字符串中的一个字符
ylbtech-Java-Runoob-高级教程-实例-字符串:03. Java 实例 - 删除字符串中的一个字符 1.返回顶部 1. Java 实例 - 删除字符串中的一个字符 Java 实例 以 ...
- C#根据用户输入字符串,输出大写字母有几个,小写字母有几个
static void Main(string[] args) { // 根据用户输入字符串,输出大写字母有几个,小写字母有几个. Console.WriteLine("请输入一行英文代码& ...
- Day_09【常用API】扩展案例5_获取长度为5的随机字符串,字符串由随机的4个大写英文字母和1个0-9之间(包含0和9)的整数组成
分析以下需求,并用代码实现 1.定义String getStr(char[] chs)方法 功能描述:获取长度为5的随机字符串,字符串由随机的4个大写英文字母和1个0-9之间(包含0和9)的整数组成 ...
- 【LeetCode】1461. 检查一个字符串是否包含所有长度为 K 的二进制子串 Check If a String Contains All Binary Codes of Size K
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计长度为 K 的子串个数 日期 题目地址:https ...
随机推荐
- thinkphp5+python.apscheduler实现计划任务
1.thinkphp5配置自定义命令行 /application/console/command namespace app\console\command; use think\console\Co ...
- 015、Java中定义变量时不设置内容,使用变量前设置内容
01.代码如下 package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 埃及分数问题 迭代加深搜索/IDA*
输入整数a,b (0<a<b<500) ,输出最佳表达式 使得加数个数尽量小,如果加数个数相同,则最小的分数越大越好 ,输出表达式 考虑从小到大枚举深度上限maxd,每次执行只考虑深 ...
- dedecms 标签使用 runphp=php 获取文章静态地址
[field:id runphp='yes'] $url=GetOneArchive(@me); @me=$url['arcurl']; [/field:id]
- Ican协议建立连接我的感悟
有一个情形我突然之间想明白了. 注意下面情形: 假设节点A与节点B已经 正常的建立了连接,并且进行了通讯. 假设 节点B收到了 节点A 的 &q ...
- 基于 Annotation 的装配(注解)
注解:就是一个类,使用@注解名称 开发中:使用注解 取代 xml配置文件. 1. @Component取代<bean class=""> @Component(&quo ...
- 中兴获25个5G商用合同
网易科技讯,6 月 25 日消息,在 2019 年 MWC 上海展期间,中兴通讯宣布随着全球首批 5G 规模商用部署展开,已在全球获得 25 个 5G 商用合同,覆盖中国.欧洲.亚太.中东等主要 5G ...
- 简单总结Get与Post的区别
工作当中经常遇到这两种类型的接口,也会被问到这两种类型的区别,这里简单总结一下算是一个简单的回忆吧. GET和POST是http协议的两种发送请求的方法.因为http的底层是TCP/IP,所以GET和 ...
- 利用 Python 破解 ZIP 或 RAR 文件密码
我们经常会从网络上下载一些带密码的压缩包,想要获取里面的内容,往往就要给提供商支付一些费用.想要白嫖其中的内容,常见的做法是百度搜索一些压缩包密码破解软件,但后果相信体验过的人都知道.本文将会利用 P ...
- 三十七、SAP中文本资源的存放
一.我们看看之前的代码内容 二.菜单转到->文本元素 三.在文本符号中写入需要替换的内容 四.修改一下代码,可以用text-001来等效替换 五.效果如下