Alphabetic Removals(模拟水题)
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.
Input
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.
Output
Print the string that will be obtained from ss after Polycarp removes exactly kkletters 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).
Examples
15 3
cccaabababaccbc
cccbbabaccbc
15 9
cccaabababaccbc
cccccc
1 1
u
题目意思:给你一个含有n个字符的字符串,删除其中的k个字符,按照字典序列删除,即abcdef......,求出删除完成之后的字符串。
方法一:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int id;///字符在字符串中的位置
int vis;///是否删除的状态
char ch;///字符
} s[];
char x[];
int my_comp1(node a,node b)///先按照字典序排序升序,字典序相同时按照在字符串位置升序
{
if(a.ch==b.ch)
{
return a.id<b.id;
}
else
{
return a.ch-'a'<b.ch-'a';
}
}
int my_comp2(node a,node b)///再按照在字符串中的位置升序
{
return a.id<b.id;
}
int main()
{
int n,k,i,len;
scanf("%d%d",&n,&k);
scanf("%s",x);
len=strlen(x);
for(i=; i<len; i++)
{
s[i].ch=x[i];
s[i].id=i;
s[i].vis=;
}
sort(s,s+len,my_comp1);
for(i=; i<k; i++)
{
s[i].vis=;
}
sort(s,s+len,my_comp2);
for(i=; i<len; i++)
{
if(s[i].vis==)
{
continue;
}
else
{
printf("%c",s[i].ch);
}
}
printf("\n");
return ;
}
方法二:成批次地按照字典序删除字符,直到删够k个结束,时间复杂度会更低
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char s[];
char a[]="abcdefghijklmnopqrstuvwxyz";
int main()
{
int n,k;
int i,j;
char ch;
while(scanf("%d%d",&n,&k)!=EOF)
{
getchar();
scanf("%s",s);
j=;
ch=a[j];
while()
{
for(i=; i<n; i++)
{
if(s[i]==ch)
{
s[i]=' ';
k--;
}
if(k==)
{
break;
}
}
ch=a[++j];
if(k==)
{
break;
}
}
for(i=;i<n;i++)
{
if(s[i]!=' ')
{
printf("%c",s[i]);
}
}
}
return ;
}
Alphabetic Removals(模拟水题)的更多相关文章
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- POJ 2014:Flow Layout 模拟水题
Flow Layout Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3091 Accepted: 2148 Descr ...
- 模拟水题,查看二维数组是否有一列都为1(POJ2864)
题目链接:http://poj.org/problem?id=2864 题意:参照题目 哈哈哈,这个题discuss有翻译哦.水到我不想交了. #include <cstdio> #inc ...
- UVA 10714 Ants 蚂蚁 贪心+模拟 水题
题意:蚂蚁在木棍上爬,速度1cm/s,给出木棍长度和每只蚂蚁的位置,问蚂蚁全部下木棍的最长时间和最短时间. 模拟一下,发现其实灰常水的贪心... 不能直接求最大和最小的= =.只要求出每只蚂蚁都走长路 ...
- Codeforces 1082B Vova and Trophies 模拟,水题,坑 B
Codeforces 1082B Vova and Trophies https://vjudge.net/problem/CodeForces-1082B 题目: Vova has won nn t ...
- HDU4287-STL模拟水题
一场2012天津网络预选赛的题,签到题. 但是还是写了三四十分钟,C++和STL太不熟悉了,总是编译错误不知道怎么解决. 一开始用的Char [] 后来改成了string,STL和string搭配起来 ...
- hdu 4891 模拟水题
http://acm.hdu.edu.cn/showproblem.php?pid=4891 给出一个文本,问说有多少种理解方式. 1. $$中间的,(s1+1) * (s2+1) * ...*(sn ...
- Mishka and Contest(模拟水题)
Mishka started participating in a programming contest. There are nn problems in the contest. Mishka' ...
- 模拟水题,牛吃草(POJ2459)
题目链接:http://poj.org/problem?id=2459 题目大意:有C头牛,下面有C行,每头牛放进草地的时间,每天吃一个草,总共有F1个草,想要在第D的时候,草地只剩下F2个草. 解题 ...
随机推荐
- 学习笔记 - 中国剩余定理&扩展中国剩余定理
中国剩余定理&扩展中国剩余定理 NOIP考完回机房填坑 ◌ 中国剩余定理 处理一类相较扩展中国剩余定理更特殊的问题: 在这里要求 对于任意i,j(i≠j),gcd(mi,mj)=1 (就是互素 ...
- Linux通过Shell脚本命令修改密码不需要交互
交互方式修改密码 1. ssh 远程到主机: 2. 切换到root账号: [一般都是切换到root进行密码修改,如果普通用户修改自己的密码,要输入原密码,然后新密码要满足复杂度才OK]: 3. pas ...
- 前端优化:css雪碧图实践应用详解
一 为什么需要使用雪碧图 二CSS雪碧图原理及应用 前端是接近用户体验的一个项目组成部分,合适的优化能够大大减少网页响应时间,合理的资源加载自然成为了工作中的要务,现在就结合实例讲解到底什么是css雪 ...
- Spark在实际项目中分配更多资源
Spark在实际项目中分配更多资源 Spark在实际项目中分配更多资源 性能调优概述 分配更多资源 性能调优问题 解决思路 为什么调节了资源以后,性能可以提升? 性能调优概述 分配更多资源 性能调优的 ...
- Zookeeper -- 关于Zookeeper
Zookeeper是什么? 分布式协调框架 Zookeeper中文件呈树形结构,树形结构下包含多个节点,称为Znode:zk中节点存储数据不超过1M,指得是Znode中存储数据不超过1M Zookee ...
- Struts2获取Servlet的api的两种方式,解决ParameterAware过时的问题
servlet API通过ActionContext进行获取 Struts2对HttpServletRequest,HttpSession和ServletContext进行了封装,构造了3个Map对象 ...
- WPF程序,运行时,结束时,要运行的操作(自动保存,检查单程序)
/// <summary> /// App.xaml 的交互逻辑 /// </summary> public partial class App : Application { ...
- 成都Uber优步司机奖励政策(2月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- apache Subversion 直接支持LDAP域群组
如果你的Subversion已经用apache的ldap支持用户认证功能,你是否常常在想,既然都用ldap支持认证,为什么不直接支持域群组, 反而在authz文件里面一个一个的手工定义,或者有人用脚本 ...
- uvaoj 213 - Message Decoding(二进制,输入技巧)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...