Minimum Lexicographic Order

Source: Internet
Author: User

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

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.