知识点:贪心
这个题第一次见还是比较难的,题意使去掉一些数,使得剩下的数最大,用贪心的思想,做题的思路先转化一下,把题目转换成从中取出多少个数,使得组成的数组大,然后开始用贪心一个找,这个思路怎么想起来的,正确性什么的都还不会,只知道使这么做的,以我们找第一位数字为例,我们给后面的数字留n-1位(假设我们要的最大数字是n位的),然后在前面的若干位里面找一个最大的,找完之后,前面的数字当然就舍弃了,然后在第一位后面按照同样的思路来找第二位第三位等等,这样的时间复杂度是n方,本题可以过,但是还有n的写法,应该是用了散列,暂时还没写出来,以后写出来了补上
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int main() {
int n, d;
while (cin >> n >> d && n) {
string s;
cin >> s;
int e = n - d;
int beg = 0;
for (int i = 1; i <= e; i++) {
int t_max = -1;
int pos;
for (int j = beg; j < (int) s.size() - e + i; j++) {
if (s[j] - '0' > t_max) {
t_max = s[j] - '0';
pos = j;
}
}
cout << s[pos];
beg = pos + 1;
}
cout << endl;
}
return 0;
}