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. C# TextBox 拖入数据 为路径

    1. 通过DragEnter事件获得被拖入窗口的“信息”(可以是若干文件,一些文字等等),在DragDrop事件中对“信息”进行解析. 2.接受拖放控件的AllowDrop属性必须设置成true; 3 ...

  2. Error: Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (64)

    错误提示: Error: Node Sass does not yet support your current environment: Windows 64-bit with Unsupporte ...

  3. javascript中继承方式及优缺点(三)

    文以<JavaScript高级程序设计>上的内容为骨架,补充了ES6 Class的相关内容,从我认为更容易理解的角度将继承这件事叙述出来,希望大家能有所收获. 1. 继承分类 先来个整体印 ...

  4. python3基础: 元组tuple、 列表list、 字典dict、集合set。 迭代器、生成器

    一.元组: tuple Python 的元组与列表类似,不同之处在于元组的元素不能修改. 元组中的元素值是不允许删除的,但我们可以使用del语句来删除整个元组 tup2 = (111, 22, 33, ...

  5. 分治NTT:我 卷 我 自 己

    感觉这种东西每次重推一遍怪麻烦的,就写在这里了. 说白了就是根据分治区间左端点是否为\(0\)分类讨论一下,一般是如果不是\(0\)就要乘\(2\),不过还是需要具体问题具体分析一下才好(就比如下面的 ...

  6. 【Linux】GDB用法详解(5小时快速教程)

    GDB是一个强大的命令行调试工具.虽然X Window提供了GDB的图形版DDD,但是我仍然更钟爱在命令行模式下使用GDB.大家知道命令行的强大就是在于,其可以形成执行序列,形成脚本. UNIX下的软 ...

  7. Git 合并两个分支内容

    1,将开发分支代码合入到master中 git checkout dev #切换到dev开发分支 git pull git checkout master git merge dev #合并dev分支 ...

  8. leetcode-easy-trees-102. Binary Tree Level Order Traversal-YES

    mycode  98.56% # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x ...

  9. C++重写实践&与java的差异

    C++重写父类方法后,父类中同名的方法在子类中就无法被调用,回报这个问题: java中是没有这个问题的,显然java设计者在设计的时候有意解决了这个问题. C++实践代码: #include < ...

  10. linux如何杀掉进程(kill)

    方法/步骤1: 使用“ps -e|grep mysql”命令,查看mysql程序的对应的pid号.结果如下图:   方法/步骤2: 使用“kill -9 2891”命令,可以结束掉mysqld_saf ...