Reprinted please indicate the source: http://blog.csdn.net/u012860063
Question link: http://poj.org/problem? Id = 3617
Question]
A string with a length of n (n <= 1000). Each time, you can take out a character from the end of the team or the first of the team and add it to the end of the new string to obtain the new string with the smallest Lexicographic Order.
[Input]
The first line is n
The next n rows have one uppercase letter per line
[Output]
New string with the smallest Lexicographic Order
Description
FJ is about to take hisN(1 ≤NLess than 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 convert al the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of) 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: LineI+ 1 contains a single initial ('A' .. 'Z') of the cow inITh position in the original line
Output
The least lexicographic string he can make. Every line (character t perhaps the last one) contains the initials of 80 cows ('A'... 'Z') in the new line.
Sample Input
6ACDBCB
Sample output
ABCBCD
Source
Usaco 2007 November silver
The Code is as follows:
# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> using namespace STD; # define PI ACOs (-1.0) # define INF 0x3fffffffint main () {int N; char C, a [2047]; int I, K, J; while (~ Scanf ("% d", & N) {k = 0; memset (A, 0, sizeof (a); for (I = 0; I <N; I ++) {getchar (); scanf ("% C", & A [I]);} int T1 = 0, T2 = n-1; // T1 is the header. T2 is the tail of for (I = 0; I ++) {if (a [T1] <A [T2]) {printf ("% C ", A [T1]); T1 ++, K ++;} else if (a [T1]> A [T2]) {printf ("% C ", A [T2]); T2 --, K ++;} else if (a [T1] = A [T2]) // If the beginning and end of the letter are the same {// then look in the middle until two different int C1, C2; C1 = T1, C2 = t2 are found; while (A [C1] = A [C2]) {C1 ++, C2 --;} if (a [C1] <A [C2]) {printf ("% C", a [T1]); T1 ++, K ++;} else {printf ("% C", a [T2]); t2 --, K ++;} If (K % 80 = 0) printf ("\ n"); If (k = N) break ;} printf ("\ n");} return 0 ;}