POJ 3617 Best Cow Line——贪心

来源:互联网 发布:云计算运维工程师待遇 编辑:程序博客网 时间:2024/06/05 20:26

注意80个字符换行

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int main() {    int n;    char str[10000], ans[10000];    scanf("%d", &n);    for (int i = 0; i < n; i++) {        getchar();        scanf("%c", &str[i]);    }    int L = 0, R = n - 1, cnt = 0;    while (L <= R) {        if (str[L] < str[R]) {            ans[cnt++] = str[L];            L++;        }        else if (str[L] > str[R]) {            ans[cnt++] = str[R];            R--;        }        else {            int tl = L, tr = R;            while (tl <= tr && str[tl] == str[tr]) {                tl++;                tr--;            }            if (tl <= tr) {                if (str[tl] < str[tr]) {                    ans[cnt++] = str[L];                    L++;                }                else if (str[tl] > str[tr]) {                    ans[cnt++] = str[R];                    R--;                }            }            else {                ans[cnt++] = str[L];                L++;            }        }    }    for (int i = 0; i < n; i++) {        printf("%c", ans[i]);        if ((i + 1) % 80 == 0) printf("\n");    }}