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 ;
}
最新文章
- Redis学习手册(目录)
- Java内部类学习笔记
- java数组
- iOS界面传值的方式(7种)
- centos6.5分区简易操作
- [Ubuntu] ubuntu13.04 从php5.4降级到php5.3
- Redis源码研究--启动过程
- zencart技术联盟交流群
- VS中无法加入断点进行调试解决方案
- 怎样查看apk须要支持的Android版本号
- AngularJS中如果ng-src 图片加载失败怎么办
- Node.js + React + MongoDB 实现 TodoList 单页应用
- 分布式存储ceph——(4)ceph 添加/删除osd
- 大数据小白系列——HDFS(4)
- python redis模块详解
- es6(8)--对象
- PHP实现图片批量压缩
- 推文《阿里凑单算法首次公开!基于Graph Embedding的打包购商品挖掘系统解析》笔记
- C# string.Format(";{0:C3}";, 2)
- 在navicat中新建数据库
热门文章
- 033.SAP上查看IDOC接口,PI接口查不到的日志记录,可能在IDOC接口日志里面
- 017.Oracle数据库,取今年第一天,取今年最后一天
- docker 容器启动时设置环境变量source
- 云服务器:西部数码VS阿里云
- 51nod 1294 :修改数组 &;&; HDU 5256:序列变换
- 从ofo牵手理财平台看,用户隐私数据的使用有底线吗?
- 6 —— node —— 响应一个完整的页面
- oracle基础知识小结
- 课程作业02-1-课后作业1-(1)使用组合数公式利用n!来计算
- HDU 1003:Max Sum