hdu 3183(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183
思路:比较前后两个相邻的字符,如果前面一个字符大于后面一个字符,就把它去掉。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b): ++i)
using namespace std; const int MAX_N = (1000 + 100);
int N, M, len;
char str[MAX_N], ans[MAX_N]; int main()
{
while (cin >> str >> M) {
N = strlen(str), len = 0;
REP(i, 0, N - 1) {
if (str[i] > str[i + 1] && M) {
--M;
while (len - 1 >= 0 && ans[len - 1] > str[i + 1] && M) {
--len, --M;
}
}
else
ans[len++] = str[i];
}
ans[len++] = str[N - 1];
string tmp = "";
REP(i, 0, len - M) tmp += ans[i];
int p = 0;
N = (int)tmp.size(), len = 0;
while (p < N && tmp[p] == '0')++p;
while (p < N) ans[len++] = tmp[p], ++p;
ans[len] = 0;
if (strlen(ans) == 0) {
cout << 0 << endl;
}
else
cout << ans << endl;
}
return 0;
}
hdu 3183(贪心)的更多相关文章
- hdu 3183 贪心
题意:给一个数字,删掉其中的若干位,使得最后的数字最小 就是每次删除数的时候都是删掉第一个比右边数大的数 利用双向链表模拟 #include<cstdio> #include<ios ...
- hdu 3183 A Magic Lamp(RMQ)
题目链接:hdu 3183 A Magic Lamp 题目大意:给定一个字符串,然后最多删除K个.使得剩下的组成的数值最小. 解题思路:问题等价与取N-M个数.每次取的时候保证后面能取的个数足够,而且 ...
- hdu 3183 A Magic Lamp RMQ ST 坐标最小值
hdu 3183 A Magic Lamp RMQ ST 坐标最小值 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题目大意: 从给定的串中挑 ...
- HDU 3183.A Magic Lamp-区间找最小值-RMQ(ST)
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- A Magic Lamp HDU - 3183 (逆向贪心/RMQ)
Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so ...
- hdu 3183 A Magic Lamp 贪心
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...
- HDU 3183 - A Magic Lamp - [RMQ][ST算法]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...
- Hdu 5289-Assignment 贪心,ST表
题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 4803 贪心/思维题
http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么? G++ AC C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...
随机推荐
- Gitlab的Gravatar头像无法显示的问题
通过gitlab搭建的git仓库,由于Gravatar被墙了,导致Gravatar头像无法显示.总觉得怪怪的. 社区版gitlab解决办法: vi /var/opt/gitlab/gitlab-rai ...
- 使用Java数组实现双色球选号
package com.hm.test; import java.util.Random; /** * 模拟双色球生成 * *1.从1到16中产生一个篮球的随机数 *2.从1到33中产生出6个红色的球 ...
- ios cordite 读取错误CoreData could not fulfill a fault for '0x15b4a870
解释在这里 http://stackoverflow.com/questions/14296892/nsobjectinaccessibleexception-reason-coredata-coul ...
- INNODB自增主键的一些问题
背景: 自增长是一个很常见的数据属性,在MySQL中大家都很愿意让自增长属性的字段当一个主键.特别是InnoDB,因为InnoDB的聚集索引的特性,使用自增长属性的字段当主键性能更好,这里要说明下自增 ...
- gtk+-3.21.4 static build step in windows XP
In recent days the weather is very hot Unable to sleep properly Under the state of daze research gtk ...
- Kinect SDK 安装失败
错误提示:Kinect Management failed to start. 原因: 1, Kinect Management 服务相依的 Plug and Play的服务没有启动. 2,系统安装了 ...
- ABAP 内表的行列转换
http://www.cnblogs.com/qlp1982/p/3370591.html
- bind+dlz+mysql实现区域记录动态更新
BIND-DLZ实验:http://bind-dlz.sourceforge.net/ 实验环境:RHEL4,BIND-9.5.0-P2.tar.gz(9.4.0以上版本都已含DLZ补丁),Mysql ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- nohup命令
nohup就是不挂起的意思( n ohang up). .nohup command 或者 nohup command & 这之间的差别是带&的命令行,即使terminal(终端)关闭 ...