String Construction
InterviewTime limit1sMemory limit128 MB
Repeatedly take the leftmost or rightmost character of S and append it to T; among all such T, output the lexicographically smallest, wrapping lines at 80 characters.
- Level
Medium5 of 10
- Topics
- Greedy, String, Two pointers, Implementation
- Solved
- No attempts yet
Problem
A string of length is given.
Using the characters of , build a new string . Initially is empty. Repeat one of the following two operations until becomes empty:
- Remove the first (leftmost) character of and append it to the end of .
- Remove the last (rightmost) character of and append it to the end of .
Among all strings that can be produced this way, write a program that finds the lexicographically smallest one.
Input
The first line contains the length of the string . ()
Each of the next lines contains one character of , given in order.
Output
Print the lexicographically smallest string that can be produced. Insert a line break after every 80 characters.
Hint
Starting from ACDBCB, one way to build proceeds as follows: