码迷,mamicode.com
首页 > 其他好文 > 详细

hdu A Magic Lamp

时间:2015-11-19 14:28:29      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

http://acm.hdu.edu.cn/showproblem.php?pid=3183

A Magic Lamp

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2556    Accepted Submission(s): 999


Problem 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
 
题目大意:给一个数,删除其中的m位是其值变得最小,求删除后最小值
 
看了样例之后,以为只要删除m个较大的数,就看可以了, 结果一直wa,后来发现 21435  1这组数据,
 
按照这个思路删掉最大的一个数5得到结果2143,发现这不是最小的结果,1435才是最小的结果,这种思路是不对的,
 
那么要让结果最小,那么如果高位数字比低位数字大的话,就删除这个高位数字,这样就让结果尽可能的小了
 
比如这个例子:
 
235564  3
 
要删除m = 3个数,发现2<3,3<5,5<=5,5<6,高位都不大于低位,而6>4,这时删去6(m = 2),23554, 发现5>4,删去5(m = 1)2354,
 
 发现5>4,删去5(m = 0)234,最终结果为234,这样循环遍历,具体看代码
 
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>

using namespace std;

typedef long long ll;
const int N = 1010;

int main()
{
    char s[N];
    int a[N], str[N];
    int m, i;
    while(~scanf("%s%d", s, &m))
    {
        memset(str, 0, sizeof(str));
        int k = 0, l = strlen(s);
        for(i = 0 ; s[i] != \0 ; i++)
            a[i] = s[i] - 0;
        for(i = 0 ; i < l && m > 0; i++)
        {
            if(a[i] <= a[i + 1])
                str[k++] = a[i];
            else
            {
                m--;
                while(str[k - 1] > a[i + 1] && m > 0 && k >= 1)
                {
                    m--;
                    k--;
                }
            }
        }
        for(; i < l ; i++)
            str[k++] = a[i];
        for(i = 0 ; i < k - m ; i++)
            if(str[i] != 0)
                break;
        if(i == k - m)
            printf("0");
        else
        {
            for(int j = i ; j < k - m ; j++)
                printf("%d", str[j]);
        }
        printf("\n");
    }
    return 0;
}

 

 
 

 

hdu A Magic Lamp

标签:

原文地址:http://www.cnblogs.com/qq2424260747/p/4977269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!