Files
Data-Structure/Algorithm/Greedy/P1106 删数问题.cpp
2025-09-21 12:21:50 +08:00

40 lines
678 B
C++

#include <iostream>
#include <string>
#include <deque>
using namespace std;
int main() {
string s;
int k;
cin >> s >> k;
deque<char> q;
for(char curr : s) {
while(!q.empty() && k > 0 && q.back() > curr){
q.pop_back();
k--;
}
q.push_back(curr);
}
// 如果还有剩余 k 个数字,删掉栈尾
while(k > 0 && !q.empty()) {
q.pop_back();
k--;
}
// 拼接结果
string res;
for(char c : q) res += c;
// 去掉前导零
int i = 0;
while(i < res.size() && res[i] == '0') i++;
res = res.substr(i);
if(res.empty()) res = "0";
cout << res << endl;
return 0;
}