POJ 3617 Best Cow Line

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

題目大意:

給定一個初始字符串, 每次將這個字符串第一個和最後一個字符中較小的那一個構成新字符串的尾端, 重複這種操作直至構成一個同長度的新字符串。

解題思路:

對於新字符串來說, 有歧義的選擇就只有首尾相等的情況。 如果後面存在不相等的兩個對應的字符, 我們要先選擇離字典序大的字符近的那一個。

                 舉個例子:

                 BDACB         答案應該爲        BBCAD

                 思路很簡單, 不過這道題我PE了十幾次。。。再一次感受到了好好讀題的重要性。。。 每80個字符換一行!!! 下次要好好讀題在寫題了, 血的教訓。。。

代碼:

#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
using namespace std;
/*tools:
 *ios::sync_with_stdio(false);
 *freopen("input.txt", "r", stdin);
 */
typedef long long ll;
typedef unsigned long long ull;
const int dir[9][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1, 0, 0};
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = 1000000;
const int Max = (int) 2e4 + 7;
int n;
char str[Max];
int main() {
    //freopen("input.txt", "r", stdin);
    // initialization
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf(" %c", &str[i]);
    }
    // check the answer
    int i, j, cnt = 1;
    for (int i = 0, j = n - 1; i <= j; ) {
        bool l = 0;
        for (int k = 0; i + k <= j; ++k) {
            if (str[i + k] < str[j - k]) {
                l = 1;
                break;
            } else if (str[i + k] > str[j - k]){
                l = 0;
                break;
            }
        }
        if (l) printf("%c", str[i++]);
        else printf("%c", str[j--]);
        if ((cnt++) % 80 == 0) printf("\n");
    }
    if (n % 80) printf("\n");
    //printf("%d\n", cnt);
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章