hdu 3183(贪心)

题目链接: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;
}
上一篇:Asp.net中存储过程拖拽至dbml文件中,提示无法获得返回值


下一篇:结合中断上下文切换和进程上下文切换分析Linux内核的一般执行过程