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. python生成手机号

    """ 电信号段:133/153/180/181/189/177; 联通号段:130/131/132/155/156/185/186/145/175; 移动号段:134/ ...

  2. DVWA--File Inclusion

    0x01了解什么叫file inclusion File Inclusion,意思是文件包含(漏洞),是指当服务器开启allow_url_include选项时,就可以通过php的某些特性函数(incl ...

  3. jQuery 全选和反选demo

    前段时间做了一个全选和反选的功能,最近不忙了,做了一个简化版的demo. 全部代码如下: <!DOCTYPE html> <html> <head> <tit ...

  4. vue项目内嵌入到app input type=file 坑(文件上传插件)

    w问题描述: 我用vue-cli完成的一个移动端项目,内嵌到app当中,用原生的input type=file 来完成文件上传.在安卓下没有问题但是在苹果手机 上传第二次手机就会发生白屏 并无缘无故跳 ...

  5. web长时间不激活 终止用户回话

    参考资料: http://web.jobbole.com/89072/ http://www.cnblogs.com/1175429393wljblog/p/5570606.html http://w ...

  6. IDEA 创建maven jar、war、 pom项目

    创建java jar.pom项目时创建maven-archetype-quickstart 创建java war项目时创建maven-archetype-webapp

  7. MySQL 数据库 常用函数

    一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x) 返回大于或等于x的最小整数 ...

  8. 从txt导入数据到mysql

    当要往mysql的table中录入数据量大的时候,直接从txt录入已有数据是一个愉快的选择. 在录入数据前要做一些格式上的准备 1. txt编码要是utf-8,无BOM 2. 每行以\t\n结尾,每列 ...

  9. 前端必须掌握的 nginx 技能(2)

    概述 作为一个前端,我觉得必须要学会使用 nginx 干下面几件事: 代理静态资源 设置反向代理(添加https) 设置缓存 设置 log 部署 smtp 服务 设置 redis 缓存(选) 下面我按 ...

  10. Linux_RHEL_设置网络

    目录 目录 Selinux Iptable NetworkManager 基本网络配置 编辑网卡子接口 ip指令 ifconfig指令 修改主机名 服务端口 Selinux 是Linux的一种权限管理 ...