POJ3617 - Best Cow Line(贪心)(让字典序序列尽可能小)

 
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 49924   Accepted: 12705

Description

FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges.

The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (i.e., If FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD). After the registration phase ends, every group is judged in increasing lexicographic order according to the string of the initials of the cows' names.

FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange his cows, who have already lined up, before registering them.

FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) original line to the end of the new line. When he's finished, FJ takes his cows for registration in this new order.

Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.

Input

* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single initial ('A'..'Z') of the cow in the ith position in the original line

Output

The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the new line.

Sample Input

6
A
C
D
B
C
B

Sample Output

ABCBCD

Source

USACO 2007 November Silver

题意:给你一个由n个字母组成的字符串S和一个空字符串T

    你有两种操作:

    1.从S的头部删除一个字符, 放入T的尾部;

    2/从S的尾部删除一个字符, 放入T的尾部;

    目标是要构造字典序尽可能小的字符串T.


思路:只要一直选小的字母加到字符串T里面就可以了

代码:
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <string>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
//#include <unordered_map>
#define Fbo friend bool operator < (node a, node b)
#define mem(a, b) memset(a, b, sizeof(a))
#define FOR(a, b, c) for (int a = b; a <= c; a++)
#define RFOR(a, b, c) for (int a = b; a >= c; a--)
#define off ios::sync_with_stdio(0)
#define sc(a) scanf("%d",&a)
#define pr(a) printf("%d\n",a);
#define SC(n,m) scanf("%d%d",&n,&m)
bool check1(int a) { return (a & (a - 1)) == 0 ? true : false; }

using namespace std;
typedef pair<int, int> p;
typedef long long ll;
const int INF = 0x3f3f3f3f;//1e10
const int mod = 1e9 + 7;
const int Maxn = 5005;
const int M = Maxn * 20;
const double pi = acos(-1.0);
const double eps = 1e-8;

int main() {
    int n;
    string s;
    cin >> n;
    FOR(i, 0, n-1) {
        char x;
        cin >> x;
        s += x;
    }
    ll cnt = 0;
    ll i=0, j=n-1;
    while (i <= j) {
        bool ok = false;
        for (int k = 0; k + i <= j;k++) {
            if (s[i+k] > s[j-k]) {
                ok = true;
                break;
            }
            else if (s[i+k] < s[j-k]) {
                ok = false;
                break;
            }
        }
        if (ok) {
            if (cnt % 80 == 0&&cnt!=0)cout << endl;
            cout << s[j--];
            cnt++;
        }
        else {
            if (cnt % 80 == 0&&cnt!=0)cout << endl;
            cout << s[i++];
            cnt++;
        }
    }
}

 

posted @ 2020-04-21 16:30  AlexLIN·  阅读(181)  评论(0)    收藏  举报