atcoder#ARC134B. [ARC134B] Reserve or Reverse
[ARC134B] Reserve or Reverse
Score : points
Problem Statement
Given is a string of length . Let denote the -th character of .
Snuke will transform by the following procedure.
- Choose an even length (not necessarily contiguous) subsequence of ( may be ).
- Swap and .
- Swap and .
- Swap and .
- Swap and .
Among the strings that can become after the procedure, find the lexicographically smallest one.
What is the lexicographical order?
Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. As a more formal definition, here is the algorithm to determine the lexicographical order between different strings and .
Below, let denote the -th character of . Also, if is lexicographically smaller than , we will denote that fact as ; if is lexicographically larger than , we will denote that fact as .
- Let be the smaller of the lengths of and . For each , we check whether and are the same.
- If there is an such that , let be the smallest such . Then, we compare and . If comes earlier than in alphabetical order, we determine that and quit; if comes later than , we determine that and quit.
- If there is no such that , we compare the lengths of and . If is shorter than , we determine that and quit; if is longer than , we determine that and quit.
Constraints
- is a string of length consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
Output
Print the lexicographically smallest possible string after the procedure by Snuke.
4
dcab
acdb
- When , the procedure just swaps and .
- The after the procedure is
acdb
, the lexicographically smallest possible result.
2
ab
ab
- When , the after the procedure is
ab
, the lexicographically smallest possible result. - Note that may have a length of .
16
cabaaabbbabcbaba
aaaaaaabbbbcbbbc
17
snwfpfwipeusiwkzo
effwpnwipsusiwkzo