1640: [Usaco2007 nov]best Cow Line Queue transform

Source: Internet
Author: User

1640: [Usaco2007 nov]best Cow line Queue transform time limit:5 Sec Memory limit:64 MB
submit:543 solved:278
[Submit] [Status] Description

FJ intends to take his lovely N (1≤n≤2,000) cow to the "best old farmer of the Year" competition. During the race, each farmer lined up his cows and was prepared to undergo a jury test. The game simply abbreviated the name of the cow to its first letter (the initial letters of every cow), for example, FJ with Bessie, Sylvia, and Dora, then it can be abbreviated to BSD. FJ just rearrange a sequence of cows and compete. He can get the first cow in the sequence, or the last one, to come out and stand at the end of the new queue. Greed's FJ in order to win the championship, he must make the new queue as small as possible dictionary. Give you the initial cow sequence (with the head letter), then make a new sequence according to the above rules, and make the dictionary order of the new sequence as small as possible.

Input

Line 1th: an integer n.

Line 2nd to n+1: One uppercase letter per line representing the first letter of the cow in the initial sequence.

Output

Gets the smallest sequence of dictionary orders. 80 letters per output need a line break!

Sample Input6
A
C
D
B
C
B
Sample OutputABCBCD
HINT

Source

Silver

The problem: hehe, the question is directly recursive on the line, pay attention to deal with when the two sides equal when the situation (really is the weakening version, simple recursive 40ms I was drunk)

1 var2 I,j,k,l,m,n:longint;3 s1:ansistring;4 C1:char;5 functionConcern (s1:ansistring): ansistring;6          var7 I,j:longint;8          begin9               ifLength (S1) =1  Thenexit (S1);Teni:=1; j:=length (s1); One                while(I<J) and(S1[i]=s1[j]) Do A                     begin - Inc (i); - Dec (j); the                     End; -               ifS1[I]<=S1[J] Then -concern:=s1[1]+concern (Copy (S1,2, Length (S1)-1)) -               Else +Concern:=s1[length (S1)]+concern (copy (S1,1, Length (S1)-1)); -          End; + begin A READLN (n); ats1:="'; -       fori:=1  toN Do -          begin - READLN (C1); -s1:=s1+C1; -          End; ins1:=concern (S1); -       fori:=1  toLength (S1)Div  the  Do toWriteln (Copy (s1,i* the- -, the)); +      if(Length (S1)MoD  the) >0  Then -Writeln (Copy (S1,length (S1) +1-(Length (S1)MoD  the), Length (S1)MoD  the)); the End.

1640: [Usaco2007 nov]best Cow Line Queue transform

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.