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. php重写与重载

    转载:https://blog.csdn.net/binghui1990/article/details/9105237 重写/覆盖 override   指:子类重写了父类的同名方法 (注:1.重写 ...

  2. 51 Nod 不重叠的线段

    #include<bits/stdc++.h> #define in(X) scanf("%d",&X) #define out(X) printf(" ...

  3. eclipse中把选中的代码全部变成大写或者小写的快捷键

    Ctrl+shift+x是把选中的变成大写 Ctrl+shift+y是把选中的变成小写

  4. js创建对象的6种方式总结

    1.new 操作符 + Object 创建对象 var person = new Object(); person.name = "lisi"; person.age = 21; ...

  5. 第七周课程总结 & 实验报告(五)

    第七周课程总结 一.抽象类与接口的应用 1.实例化 2.实际应用 ---模板设计(抽象类) ---制定标准(接口) 3.设计模式 ---工厂设计 ---代理设计 ---适配器设计 二.抽象类与接口之间 ...

  6. leetcode-easy-listnode-234 Palindrome Linked List

    mycode   89.42% # Definition for singly-linked list. # class ListNode(object): # def __init__(self, ...

  7. 引用&指针交换函数实践

    实践如下: #include <iostream> using namespace std; // 普通交换,注意这里的ab值,在具体调用时是基本数据的拷贝,原始数据不会变化 // 因此这 ...

  8. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  9. kotlin之基本数据类型

    数据类型                       占用字节数 Double 8 Float 4 Long 4 Int 4 Short 2 Byte 1 数据类型之间的转换 toByte():转换为 ...

  10. iOS 图表工具charts之BarChartView

    关于charts的系列视图介绍传送门: iOS 图表工具charts介绍 iOS 图表工具charts之LineChartView iOS 图表工具charts之BarChartView iOS 图表 ...