HDU-3183 A Magic Lamp--贪心+暴力数组移动

Posted lonely-wind-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU-3183 A Magic Lamp--贪心+暴力数组移动相关的知识,希望对你有一定的参考价值。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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个数字,使得最终的结果最小,你不能改变顺序。
其实挺好想的,要使得最终结果最小,那么我们只需要每次删去下降的那个点就好了,如178543 4:
8->5下降,那么我们删去8,剩下17543 3
7->5下降,我们删去7,剩下1543 2
5->4下降,删去5,剩下143 1
4->3下降,删去4,剩下13 0
然后结束了。
当然,如果删完之后m还剩余,且数列递增,那么我们就从后往前删就好了,最后记得去掉前导零就OK了。
以下是AC代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int mac=1e3+10;

char s[mac];

void solve(char *s,int pos,int n)
{
    for (int i=pos; i<n; i++)
        s[i]=s[i+1];
}

int main()
{
    int m,n;
    while (~scanf("%s",s)){
        scanf("%d",&m);
        n=strlen(s);
        for (int i=0; i<n-1; i++){
            if (!m) break;
            while (s[i]>s[i+1] && m) solve(s,i,n--),m--,i--;
        }
        while (m){
            m--;s[--n]=;
        }
        int len=strlen(s);
        int i=0;
        while(s[i]==0) i++;
        for (int j=i; j<len; j++) printf("%c",s[j]);
        if (i==len) printf("0");
        printf("
");
    }
    return 0;
}

 

 

 

以上是关于HDU-3183 A Magic Lamp--贪心+暴力数组移动的主要内容,如果未能解决你的问题,请参考以下文章

hdu-3183A Magic Lamp (RMQ)

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

A Magic Lamp HDU - 3183(RMQ返回下标)

hdu 3183 A Magic Lamp(给一个n位的数,从中删去m个数字,使得剩下的数字组成的数最小(顺序不能变),然后输出)

PAT Advanced 1037 Magic Coupon (25) [贪?算法]

HDU 3183 贪心