#C6525. Largest Number by Adjacent Swaps
Largest Number by Adjacent Swaps
Largest Number by Adjacent Swaps
Given a numeric string S
composed solely of digits, your task is to form the largest possible number by performing an arbitrary number of adjacent swaps. In each swap, you may only exchange two consecutive digits.
The problem can be mathematically described as follows. Let \( S = d_1d_2\ldots d_n \) be the given string. You are allowed to perform adjacent swaps to produce a new sequence \( d'_1d'_2\ldots d'_n \) such that:
\( d'_1 \geq d'_2 \geq \ldots \geq d'_n \)
This means the resulting number is simply the digits of S
sorted in non-increasing (descending) order.
Note: The input is provided via standard input (stdin) and the output should be printed to standard output (stdout).
inputFormat
The input consists of a single line containing the string S
which represents a non-negative integer. There are no extra spaces in the input.
Example:
34912
outputFormat
Output a single line containing the largest number possible after performing any number of adjacent swaps on the input string. The resulting number will be the digits of S
arranged in non-increasing order.
Example:
94321## sample
34912
94321