For a given string s with a length of N (containing only uppercase letters), you must construct a string T with a length of N. At first, t is an empty string, and then perform any of the following operations repeatedly.
Delete a character from the S header and add it to the end of T;
Delete a character from the end of S and add it to the end of T.
The objective is to construct a string T with the smallest Lexicographic Order Possible.
Input:
The first line is a positive integer N;
N rows. Each row has one uppercase letter.
Output:
T (when the output line is full of 80 characters, the line breaks)
Sample input:
6 acdbcb
Sample output:
ABCBCD
Restrictions:
1 ≤ n ≤2000
String S contains only uppercase English letters
Interpretation: Lexicographic Order refers to the method for comparing the size of two strings from the beginning to the end. First, compare the first character. If it is different, the first character string is smaller. If it is the same, continue to compare 2nd characters ...... to compare the size of the entire string.
Analysis: greedy
Compare the positive and reverse orders of strings, and add the first letter of a small string to the end of T each time!
var n:integer; t,s1,s2:ansistring; ch:char; procedure init; var i:integer; begin readln(n); s1:=‘‘;s2:=‘‘; for i:=1 to n do begin readln(ch); s1:=s1+ch; s2:=ch+s2; end; end; procedure main; var i:integer; begin for i:=1 to n do if s1<s2 then begin t:=t+s1[1]; delete(s1,1,1); delete(s2,n-i+1,1); end else begin t:=t+s2[1]; delete(s1,n-i+1,1); delete(s2,1,1); end; end; procedure print; var i:integer; begin for i:=1 to n do begin write(t[i]); if i mod 80 =0 then writeln; end; end;begin init; main; PRINT;end.
Minimum Lexicographic Order