POJ 3617 Best Cow Line

Source: Internet
Author: User

http://poj.org/problem;jsessionid=F0726AFA441F19BA381A2C946BA81F07?id=3617

Description

FJ is an about-to-take he N (1≤ n ≤2,000) cows to the annual ' Farmer of the Year ' competition. In this contest every farmer arranges he 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 would appear (i.e., If FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD). After the registration phase ends, every group was judged in increasing lexicographic order according to the string of the Initials of the cows ' names.

FJ is very busy this year and have to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange him cows, who has 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 I n the (remainder of the) original line to the end of the new line. When he's finished, FJ takes his cows for registration on this new order.

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

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 i-th position in the original line

Output

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

Sample Input

6ACDBCB

Sample Output

Abcbcd


The problem is that a person has a group of cows, to go to the game, every cow has a name, registration only register the first letter of the name, but this person on the day of the race to go back, so want to race first, and the game is arranged according to the dictionary order, so its dictionary order the smaller the more rely on
Before, this person to form a relatively small string, he can put the original string of the head deleted at the end of the new string, you can also put the tail of the original string is deleted at the new tail, roughly this meaning
And then the book says as long as the front is smaller, and then it is constantly to take the beginning and the end of the new string to put the tail,
The book also said that the dictionary order comparison s and inverted string s ', if s is relatively small from the beginning of S to take a character, if s ' is relatively small, take s '
But!!!! I understand the wrong AH!!! I understood it as a string reversal, and then for each of the comparisons, the smaller ones took out
It's ACDBCB.
BCBDCA after reversal
Then A and B ratio, a relatively small, take a,c and C ratio, take c,d and b than take B, and then get the ACBBCA, hey, I have been tangled why the answer is not right

#include <cstdio>
int main ()
{
    int n;
    Char s[2000],c[2000];
    Char t[2000];
    scanf ("%d", &n);
    GetChar ();//input%d (that is, the number) must have a carriage return or a space you want to hit, then the buffer has \ n or a space character left,%c read is read from the buffer, then the first read is \ n, is wrong, GetChar () read out the legacy.
    for (int i=0;i<n;i++)
    {
         scanf ("%c", &s[i]);
   }
    for (int i=n-1;i>=0;i--)
    {

c[n-i-1]=s[i];//This I test the time has been output less one, the original is because GetChar ()
}
for (int i=0;i<n;i++)
{
if (S[i]<=c[i])
T[i]=s[i];
if (S[i]>c[i])
T[i]=c[i];
}
for (int i=0;i<n;i++)
printf ("%c", T[i]);
printf ("\ n");
return 0;
}

It's a lot of writing, but it's not right, but it's okay, at least you wrote it.

#include <cstdio>
int main ()
{
int n;
Char s[2000];
scanf ("%d", &n);
GetChar ();///input%d (that is, the number) must have a carriage return or a space you want to hit, then the buffer has \ n or a space character left,%c read is from the buffer read, then the first read \ n, it is wrong, getchar () read out the left of the line
for (int i=0;i<n;i++)
{
scanf ("%c", &s[i]);
}
int a=0,b=n-1;
while (A&LT;=B)
{
BOOL Left=false;
for (int i=0;a+i<=b;i++)
{
if (S[i+a]<s[b-i])
{
Left=true;
Break
}
else if (S[a+i]>s[b-i])
{
Left=false;
Break
}
}
if (left)
Putchar (s[a++]);
Else
Putchar (s[b--]);
}
Putchar (' \ n ');
return 0;
}

POJ 3617 Best Cow Line

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.