思路:
贪心,由于数的特性,显然贪心可得正解,每次选取可选的最大值。
贪心,由于数的特性,显然贪心可得正解,每次选取可选的最大值。
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <algorithm>
#define SF(a) scanf("%d", &a)
#define PF(a) printf("%d\n", a)
#define SFF(a, b) scanf("%d%d", &a, &b)
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SFFFF(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) (int)(a).size()
#define PB push_back
#define LL long long
#define mod 10007
#define inf 100000007
#define eps 1e-12
using namespace std;
int buf[20];
int read() {
int x = 0; char ch = getchar(); bool f = 0;
while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); }
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
void write(int x) {
if (!x) { putchar(48); return; }
int l = 0; if (x < 0) putchar('-'), x = -x;
while (x) buf[++l] = x % 10, x = x / 10;
while (l) putchar(buf[l--] + 48);
}
//-------------------------chc------------------------------//
const int maxn = 1e5 + 5;
char s[maxn];
char get_max(int l, int r, int &nl) {
char ret = '0';
FOR(i, l, r + 1) {
if (s[i] > ret) ret = s[i], nl = i + 1;
}
return ret;
}
int main() {
int n, k;
while (~SFF(n, k)) {
if (n == 0 && k == 0) break;
k = n - k;
scanf("%s", s);
string ans;
int l = 0, r = n - k;
int nl;
while (k--) {
char ch = get_max(l, r, nl);
ans += ch;
l = nl;
r = n - k;
}
cout << ans << endl;
}
return 0;
}
维护一个单调栈,便于在一次扫描中得到最优解。
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <algorithm>
#define SF(a) scanf("%d", &a)
#define PF(a) printf("%d\n", a)
#define SFF(a, b) scanf("%d%d", &a, &b)
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SFFFF(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) (int)(a).size()
#define PB push_back
#define LL long long
#define mod 10007
#define inf 107
#define eps 1e-12
using namespace std;
int buf[20];
int read() {
int x = 0; char ch = getchar(); bool f = 0;
while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); }
while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f ? -x : x;
}
void write(int x) {
if (!x) { putchar(48); return; }
int l = 0; if (x < 0) putchar('-'), x = -x;
while (x) buf[++l] = x % 10, x = x / 10;
while (l) putchar(buf[l--] + 48);
}
//-------------------------chc------------------------------//
const int maxn = 100005;
char s[maxn];
int main() {
int n, k;
while (~SFF(n, k)) {
if (n == 0 && k == 0) break;
int need = n - k;
stack<char> sta;
sta.push('9');
scanf("%s", s);
FOR(i, 0, n) {
if (need - SZ(sta) + 1 == n - i) {
while (i < n) sta.push(s[i++]);
}
else {
if (s[i] > sta.top()) {
while (s[i] > sta.top() && (SZ(sta) - 1 + n - i) > need) //大的数考虑是否数够多
sta.pop();
}
if(SZ(sta) - 1 < need) sta.push(s[i]); //小的数考虑是否多了
}
}
vector<char> ans;
while (SZ(sta) != 1) {
ans.push_back(sta.top());
sta.pop();
}
for (int i = SZ(ans) - 1; i >= 0; --i)
putchar(ans[i]);
putchar('\n');
}
return 0;
}