题目
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;
}