Best Cow Line POJ - 3617

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
  • 题目倒是个水题,数据也有点水,但是题意却不容忽视,不管题目如何容易,你题意不懂,一切都是假的。
  • 一个注意点输出80个字符得换行,  我犯了一个错误就是
  • for(i=0;i<n;i++)
  • {
  • getchar();
  • scanf("%c",&str[i]);
  • }
  • 如果你只这样的话错了
  • 的加个
  • str[n]='\0';
  • 直接左右标记找就可以了。
  • #include<stdio.h>
    #include<string.h>
    #include<iostream>
    using namespace std;
    int main()
    {
        int n,r,l,i,k;
        char str[2010];
        while(scanf("%d",&n)==1)
        {
            k=0;
            for(i=0; i<n; i++)
            {
                getchar();
                scanf("%c",&str[i]);
            }
            str[n]='\0';
            l=0;
            r=strlen(str)-1;
            int k1=l,k2=r;
            while(l<=r)
            {
                if(str[k1]<str[k2])
                {
                    if(k&&k%80==0)
                        printf("\n");
                    printf("%c",str[l]);
                    k++;
                    l++;
                    k1=l;
                    k2=r;
                }
                else if(str[k1]>str[k2])
                {
                    if(k&&k%80==0)
                        printf("\n");
                    printf("%c",str[r]);
                    k++;
                    r--;
                    k1=l;
                    k2=r;
                }
                else
                {
                    k1++;
                    k2--;
                }
            }
            printf("\n");
        }
        return 0;
    }

猜你喜欢

转载自blog.csdn.net/chen_zan_yu_/article/details/84452691