POJ3617(贪心)

没啥好说的,书上原题,比较坑就是输出需要每80个换行。

//#define LOCAL

#include <cstdio>
#include <cstring>
#include <algorithm>
#define SIZE 2000

using namespace std;

int main(void){
#ifdef LOCAL
    freopen("data.in", "r", stdin);
#endif
    char map[SIZE];
    int n;

    scanf("%d", &n);
    getchar();
    for(int i = 0; i < n; i++){
        scanf("%c", &map[i]);
        //printf("%c", map[i]);
        getchar();
    }
    //printf("\n");

    int left = 0;
    int right = n - 1;
    int output = 0;

    while(left <= right){
        bool flag = false;
        char temp = map[left];
        for(int i = left, j = right; i <= j; i++, j--){
            if(map[i] > map[j]){
                temp = map[right];
                flag = true;
                break;
            }else if(map[i] < map[j])
                break;
        }

        if(flag)
            right--;
        else
            left++;

        output++;
        printf("%c", temp);
        if(output % 80 == 0)
            printf("\n");
    }

    return 0;
}
点赞