1 条题解

  • 0
    @ 2026-11-2 15:26:16

    最大的数字(Largest Number) 题解

    思路分析

    按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。

    参考代码

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int d;
        string s;
        if (!(cin >> d >> s)) return 0;
        int n = (int)s.size();
        if (d >= n) {
            cout << 0 << "\n";
            return 0;
        }
        string stk;
        stk.reserve(n);
        int rem = d;
        for (char c : s) {
            while (rem > 0 && !stk.empty() && stk.back() < c) {
                stk.pop_back();
                --rem;
            }
            stk.push_back(c);
        }
        while (rem > 0 && !stk.empty()) {
            stk.pop_back();
            --rem;
        }
        size_t pos = 0;
        while (pos + 1 < stk.size() && stk[pos] == '0') ++pos;
        cout << stk.substr(pos) << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    8812
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者