P6140 [USACO07NOV]Best Cow Line S

难度:4

贪心问题,贪心策略要想对,这种一般都是从字典序全局考虑的,不能值考虑单独一个点,

#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;

int main() {
    int n;
    cin >> n;
    string s = "";
    for (int i = 0; i < n; i++) {
        string ss;
        cin >> ss;
        s += ss;
    }
    string t = "";
    while ((int) s.size()) {
        int end = (int) s.size() - 1;
        if (s[0] < s[end]) {
            t += s[0];
            s.erase(s.begin());
        } else if (s[0] > s[end]) {
            t += s[end];
            s.erase(s.begin() + end);
        } else {
            string str = s;
            reverse(all(str));
            if (s <= str) {
                t += s[0];
                s.erase(s.begin());
            } else {
                t += s[end];
                s.erase(s.begin() + end);
            }
        }
    }
    for (int i = 0; i < (int) t.size(); i++) {
        cout << t[i] << ((i + 1) % 80 == 0 ? "\n" : "");
    }
    return 0;
}
上一篇:[USACO14OPEN]Fair Photography Bronze 题解


下一篇:Linux交叉开发环境搭建 —— 效率之源