hdu 3183(贪心)

时间:2024-07-19 12:37:20

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183

思路:比较前后两个相邻的字符,如果前面一个字符大于后面一个字符,就把它去掉。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b): ++i)
using namespace std; const int MAX_N = (1000 + 100);
int N, M, len;
char str[MAX_N], ans[MAX_N]; int main()
{
while (cin >> str >> M) {
N = strlen(str), len = 0;
REP(i, 0, N - 1) {
if (str[i] > str[i + 1] && M) {
--M;
while (len - 1 >= 0 && ans[len - 1] > str[i + 1] && M) {
--len, --M;
}
}
else
ans[len++] = str[i];
}
ans[len++] = str[N - 1];
string tmp = "";
REP(i, 0, len - M) tmp += ans[i];
int p = 0;
N = (int)tmp.size(), len = 0;
while (p < N && tmp[p] == '0')++p;
while (p < N) ans[len++] = tmp[p], ++p;
ans[len] = 0;
if (strlen(ans) == 0) {
cout << 0 << endl;
}
else
cout << ans << endl;
}
return 0;
}