HDU-3183-A Magic Lamp


3183-A Magic Lamp


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

题目链接:HDU-3183

题目大意:求给出的数字num,删去k位之后,所能得到的最小值

题目思路:刚开始乍一看,觉得是删去最大的k个数字就可以。但WA了,因为比如456302420,删去最大的4个数字之后是30220。
实际上,删除前面4个数字之后,剩下的02420更小。

所以后来采用的方法是,取begin后面k+1个数字,找到最小的数字,把最小的数字前面的都删掉。然后把begin修改为当前这个最小的数字

以下是代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include<iomanip>
using namespace std;
int e[1005];
int main()
{
    string s;
    int k;
    int begin = 0;
    while(cin >> s >> k)
    {
       memset(e,0,sizeof(e));
       begin = 0;
       while(1)
       {
           int pos = begin;
           if (k == 0) break;
           if (s.size() - pos == k)
           {
               for (int i = pos; i < s.size(); i++) e[i] = 1;
               break;
           }
           char minnum = s[begin];
           for (int i = begin; i < min(begin + k + 1,(int)s.size()); i++)
           {
               if (s[i] < minnum)
               {
                   pos = i;
                   minnum = s[i];
               }
           }
           k -= pos - begin;
           for (int i = begin; i < pos; i++)
           {
               e[i] = 1;
           }
           begin = pos + 1;
       }
        int first = 0;
        for (int i = 0; i < s.size(); i++)
        {
            if (e[i]) continue;
            if (first == 0 && s[i] == '0')
            {
                e[i] = 1;
                continue;
            }
            first = 1;
            cout << s[i];
        }
        if (!first) cout << 0;
        cout << endl;
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值