uva11491 Erasing and Winning

边读入边处理 优化了速度
一开始有想错了的地方。处理输入有点想用stringstream, 的问题在于他把字符串连续的数字作为一个整体,遇到空格才分开,所以不适用

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<cstring>
using namespace std;

const int maxn = 100005;
int s[maxn];
 
int main()
{
    int n, d;
    while (scanf("%d%d%*c", &n, &d) && n) {
        int t = 0;
        int a;
        d = n - d;
        for (int i = 0; i < n; i++) { //位数相同,贪心策略为最高位最大
            a = getchar() - '0';
            while (t && t + n - i > d && a > s[t - 1]) {
                t--;
            }
            if (t < d) {
                s[t++] = a;
            }
        }
        for (int i = 0; i < t; i++) {
            printf("%d", s[i]);
        }
        printf("\n");
    }
 
    return 0;
}

 

posted @ 2018-10-05 19:24  Erio  阅读(307)  评论(0编辑  收藏  举报