A Magic Lamp HDU - 3183 - 贪心

A Magic Lamp HDU - 3183

给定一个数字求删除 N 个数字后的最小数字。

Input

有多个测试用例。
每个测试用例将包含一个给定的 x 整数和一个整数 n (如果该整数包含m位,n将不大于m)。

Output

对于每种情况,输出一行中可以得到的最小结果。
如果结果包含前导零,忽略它。

Sample Input

100001 1
231341 2
1231123 4
2333333 4

Sample Output

1
1341
111
233

分析

贪心的思想每次删除第一个 a[i] > a[i+1],如果不存在就删除最后一个。
要注意去除前导 0,以及最小为 1 位。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e6+10,INF=0x3f3f3f3f;
string a; int n;

int main(){
//    freopen("data.in", "r", stdin);
    while(cin>>a>>n){
        while(n--){
            int j=-1;
            for(int i=0; i<a.size(); i++){
                if(a[i] > a[i+1]) { j=i; break; }
            }
            if(j!=-1) a.erase(j, 1);
            else a.erase(a.size()-1, 1);
        }
        while(a[0]=='0' && a.size()>1) a.erase(0,1);
        if(a.size()==0) a="0";
        cout<<a<<endl;
    }
    return 0;
}
posted @ 2022-09-22 23:11  HelloHeBin  阅读(39)  评论(0)    收藏  举报