#寒假1# S - A Magic Lamp

题目

https://vjudge.net/problem/HDU-3183
n位数,去掉m位,使得剩下的数字最小
在这里插入图片描述

思路

维护一个单增的栈,因为写了好多while,所以可能一不小心就有死循环了。。
每次碰见单减序列,就把前面大的数字删除

代码

#include<iostream>
#include<algorithm>
#include <cstring>
#include <cmath>
#include <stack>

#define MAXX 100005
using namespace std;
typedef long long ll;

int main()
{
    char ss[1010];
    int m;
    while (cin >> ss >> m)
    {
        int len = strlen(ss);
        stack<int> s;
        for (int i = 0; i < len; ++i)
        {
            int x = ss[i] - '0';
            if (s.empty() || m == 0)
                s.push(x);
            else
            {
                while (!s.empty() && s.top() > x && m != 0)
                {
                    s.pop();
                    m--;
                }
                s.push(x);
            }
        }
        while (!s.empty() && m)
        {
            s.pop();
            m--;
        }
        int ans[1010];
        memset(ans, 0, sizeof ans);
        int cnt = 0;
        while (!s.empty())
        {
            ans[cnt] = s.top();
            cnt++;
            s.pop();
        }

        int f = 0;
        for (int i = cnt - 1; i > 0; i--)
        {
            if (ans[i] == 0 && f == 0)
                continue;
            else
            {
                cout << ans[i];
                f = 1;
            }
        }
        cout << ans[0];
        cout << endl;
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值