题目链接: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(贪心)的更多相关文章

  1. hdu 3183 贪心

    题意:给一个数字,删掉其中的若干位,使得最后的数字最小 就是每次删除数的时候都是删掉第一个比右边数大的数 利用双向链表模拟 #include<cstdio> #include<ios ...

  2. hdu 3183 A Magic Lamp(RMQ)

    题目链接:hdu 3183 A Magic Lamp 题目大意:给定一个字符串,然后最多删除K个.使得剩下的组成的数值最小. 解题思路:问题等价与取N-M个数.每次取的时候保证后面能取的个数足够,而且 ...

  3. hdu 3183 A Magic Lamp RMQ ST 坐标最小值

    hdu 3183 A Magic Lamp RMQ ST 坐标最小值 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题目大意: 从给定的串中挑 ...

  4. 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 ...

  5. 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 ...

  6. hdu 3183 A Magic Lamp 贪心

    #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...

  7. HDU 3183 - A Magic Lamp - [RMQ][ST算法]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...

  8. Hdu 5289-Assignment 贪心,ST表

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...

  9. hdu 4803 贪心/思维题

    http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上 ...

随机推荐

  1. Light OJ 1140

    数位dp,需要记录前导0. 数位dp中需要注意统计0,00,000……这些数字. 数位dp的写法可以分为两类.由于我们通常采用记忆化搜索的方式进行dp,所以我们有一个记忆化数组. 一种是记忆化数组的意 ...

  2. (转)大数据时代下的SQL Server第三方负载均衡方案----Moebius测试

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 架构原理(Architecture) 测试环境(Environment) 安装Moebius( ...

  3. VMware ESXi客户端连接控制台时提示"VMRC控制台连接已断开...正在尝试重新连接"的解决方法

    通过vSphere Client连接到安装VMware ESXi虚拟环境的主机时,当启动操作系统,选中控制台时控制台上方提示一行"VMRC控制台的连接已断开...正在尝试重新连接" ...

  4. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  5. Match:Seek the Name, Seek the Fame(POJ 2752)

    追名逐利 题目大意:给定一个字符串S,要你找到S的所有前缀后缀数组 还是Kmp的Next数组的简单应用,但是这一题有一个BUG,那就是必须输出字符串的长度(不输出就WA),然而事实上对于abcbab, ...

  6. Js 旋转木马 轮播

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. JDBC题库

    一.    填空题 JDBC    ,是一种用于执行SQL语句的Java API,为多种关系数据库提供统一访问.它由一组用Java语言编写的类和接口组成. JDBC API:供程序员调用的接口与类,集 ...

  8. 416. Partition Equal Subset Sum

    题目: Given a non-empty array containing only positive integers, find if the array can be partitioned ...

  9. HTML标记之a标签

    一.a标签的语法 <a href=”链接目标地址” title=”注释” target=”打开链接窗口的形式”>链接显示内容</a> target值: _blank在新窗口中打 ...

  10. 【2016-10-11】Linux系统常用的关机或重启命令shutdown、reboot、halt、poweroff、init 0及init 6的联系与区别

    Linux下常用的关机/重启命令一般包括: shutdown.reboot.halt.poweroff等,当然了我们可以使用init 运行等级runlevel 0即halt来关机,或使用init 运行 ...