#P6388. Lexicographically Minimal Reversed Partition

    ID: 19604 Type: Default 1000ms 256MiB

Lexicographically Minimal Reversed Partition

Lexicographically Minimal Reversed Partition

Given a non-empty string S, partition it into three non-empty segments S1, S2 and S3 such that S = S1 || S2 || S3. For each partition, reverse every segment individually to obtain S1R, S2R, and S3R, and then concatenate the results in the original segment order.

Since there are multiple possible ways to partition the string, you are required to select the one that yields the lexicographically smallest resultant string. Formally, if a candidate output corresponding to a partition is given by:

output=S1R+S2R+S3R,\text{output} = S_1^R + S_2^R + S_3^R,

then among all valid partitions, choose the candidate output such that

output=min{S1R+S2R+S3R},\text{output} = \min\{S_1^R+S_2^R+S_3^R\},

where the minimum is taken in dictionary (lexicographic) order.

inputFormat

The input consists of a single line containing the string S to be partitioned. The string contains only visible ASCII characters and its length is at least 3.

Format:

S

outputFormat

Output the lexicographically smallest string that can be obtained by reversing each of the three segments (obtained by any valid partition of S) and concatenating them in order.

Format:

result

sample

abc
abc