Description

Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so kind. Kiki must answer a question, and then the genie will realize one of her dreams.
The question is: give you an integer, you are allowed to delete exactly m digits. The left digits will form a new integer. You should make it minimum.
You are not allowed to change the order of the digits. Now can you help Kiki to realize her dream?

Input

There are several test cases.
Each test case will contain an integer you are given (which may at most contains 1000 digits.) and the integer m (if the integer contains n digits, m will not bigger then n). The given integer will not contain leading zero.

Output

For each case, output the minimum result you can get in one line.
If the result contains leading zero, ignore it.

Sample Input

178543 4
1000001 1
100001 2
12345 2
54321 2

Sample Output

13
1
0
123
321

题目分析

题意很简单,给你一个字符串,在这个字符串中删除指定数量的字符,而且不改变字符串的字符之间的顺序,使得删除指定数量的字符后的字符串所构成的数最小。

写这个题的时候首先想到的就是不可以暴力,要是枚举每一种情况的话,那应该会超时。为了找到这个题所需的算法,我手动计算了几组数据:798194 2 以及 127299 2 ,得到的答案分别是7194 和 1229 。 首先对于每一组数据,删除相同数量的字符后,这些数字的位数都是一样的,那么我们就要使得高位的字符为最小,那么对于798194这组数据,7和9比,7更小,所以没必要删除7,因为删除了7的话,这个字符串的最高位的字符值就变大了,然后我们比较第二位9和第三位8,因为第二位数大于第三位数,所以删除9的话可以让第二位数变得更小,这样是划算的,由于删除了某个位上的数,所以我们需要从头重新开始比较。

综上,我们每次比较字符串中相邻的两个字符,如果高位上的字符(比如89中,8的位就是比9的位高)大于低位上的字符,那么就删除高位上的字符,这样可以让低位上更小的字符代替高位的字符。而由于删除某个字符串,所以我们需要再次从最高位开始重复相邻位之间的比较,直到删除指定数量的字符为止。

代码区

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
const int max3 = 1000 + 10; int main()
{
char str[max3];
int n;
while(~scanf("%s%d",str,&n))
{
const int len = strlen(str);
int index = 0;
for(int i = 0 ; i < len ; i++)
{
if (!n)
break;
if (str[i] == '\0' || i < 0 )continue;
int k = i + 1;
char order = str[k];
while (order == '\0' && k < len)
order = str[++k]; if (k == len || str[i] > order) //表示高位的数字大于低位的数字或者k = len说明字符串已经按升序排序,那么删除最后一个即可
{
str[i] = '\0';
i = - 1;
n--;
}
}
bool ok = false;
for(int i = 0 ; i < len ; i++)
{
if(str[i] != '\0')
{
if(str[i] != '0' || ok)
{
printf("%c", str[i]);
ok = true;
}
}
}
if (!ok)
printf("0");
cout << endl;
}
return 0;
}

HDU 3182 ——A Magic Lamp(思维)的更多相关文章

  1. hdu 3183 A Magic Lamp(RMQ)

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

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

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

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

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

  4. hdu 3183 A Magic Lamp rmq或者暴力

    A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Pro ...

  5. hdu 3183 A Magic Lamp(RMQ)

    A Magic Lamp                                                                               Time Limi ...

  6. HDU 3183 A Magic Lamp(RMQ问题, ST算法)

    原题目 A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. HDU 3183 A Magic Lamp

    直接模拟   如果后一位比前一位小,那就一直 向前 pop()掉 维护他单调递增: #include<iostream> #include<cstring> #include& ...

  8. HDU 3183 A Magic Lamp(二维RMQ)

    第一种做法是贪心做法,只要前面的数比后面的大就把他删掉,这种做法是正确的,也比较好理解,这里就不说了,我比较想说一下ST算法,RMQ的应用 主要是返回数组的下标,RMQ要改成<=(这里是个坑点, ...

  9. hdu 3183 A Magic Lamp 【RMQ】

    <题目链接> <转载于 >>>  > 题目大意: 给出一个长度不超过1000位的数,求删去m位数字以后形成的最小的数字是多少. 解题分析: 分析:我们可以把题 ...

随机推荐

  1. ABP core2.2错误笔记,持续更新

    注:以下问题全部基于版本 © 2019 MLCDZ. Version 4.3.0.0 [20190830]  .net core 的版本为2.2 1.System.InvalidOperationEx ...

  2. Linux下测试CPU性能

    一.安装stress服务 1.下载stress_1.0.1.orig.tar.gz安装包 2.解压tar xvf stress_1.0.1.orig.tar.gz 3.进入解压目录执行./config ...

  3. 剑指offer:把一个支付算转化为整数

    1:首先,根据课本上的程序,是这样的: #include "stdafx.h" #include "iostream" using namespace std; ...

  4. mysql 判断时间 语法

    今天  select * from 表名 where to_days(时间字段名) = to_days(now());  昨天  SELECT * FROM 表名 WHERE TO_DAYS( NOW ...

  5. eclipse中设置tab为4个空格

    1.insert space for tabs前打勾 2.General settings中选择Spaces only 3.搞定

  6. winform Timer控件的使用

    private void button1_Click(object sender, EventArgs e){ Timer timer1 = new Timer(); timer1.Interval ...

  7. 一、基础篇--1.1Java基础-equals与==的区别

    ==: ==比较的是变量内存中存放的对象的内存地址,用来判断两个对象地址是否相同,比较的是否是同一个对象. 1.两边的操作数必须是同一类型,不然编译不通过. 2.如果是基本数据类型比较,值相等则为tr ...

  8. 8.6培训 D1

    今天是赵和旭老师讲课(也是 zhx) 动态规划 利用最优化原理把多阶段过程转化为一系列单阶段问题,利用各阶段之间的关系,逐个求解(有点像分治?) 更具体的,假设我们可以计算出小问题的最优解,那么我们凭 ...

  9. Android 编程下Touch 事件的分发和消费机制和OnTouchListener,OnClickListener和OnLongClickListener的关系

    1.事件分发:public boolean dispatchTouchEvent(MotionEvent ev) Touch 事件发生时 Activity 的 dispatchTouchEvent(M ...

  10. 第一篇 python数据类型

    IO文件输出 问题1:Python如何实现print不换行? 默认情况下Python的print()函数是换行的,如 print("你好大成") print("!!!&q ...