#abc242b. B - Minimize Ordering

B - Minimize Ordering

Score : 200200 points

问题描述

给定一个字符串 SS,找出通过重新排列 SS 的字符得到的字典序最小的字符串 SS'

这里,对于两个不同的字符串 s=s1s2sns = s_1 s_2 \ldots s_nt=t1t2tmt = t_1 t_2 \ldots t_m,若满足以下任一条件,则认为 s<ts \lt t 在字典序上成立:

  • 存在一个整数 i (1imin(n,m))i\ (1 \leq i \leq \min(n,m)),使得 si<tis_i \lt t_i 并且对于所有整数 j (1j<i)j\ (1 \leq j < i),都有 sj=tjs_j=t_j
  • 对于所有整数 i (1imin(n,m))i\ (1 \leq i \leq \min(n,m)),有 si=tis_i = t_i,并且 n<mn \lt m

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a string SS. Find the lexicographically smallest string SS' obtained by permuting the characters of SS.

Here, for different two strings s=s1s2sns = s_1 s_2 \ldots s_n and t=t1t2tmt = t_1 t_2 \ldots t_m, s<ts \lt t holds lexicographically when one of the conditions below is satisfied.

  • There is an integer i (1imin(n,m))i\ (1 \leq i \leq \min(n,m)) such that si<tis_i \lt t_i and sj=tjs_j=t_j for all integers j (1j<i)j\ (1 \leq j \lt i).
  • si=tis_i = t_i for all integers i (1imin(n,m))i\ (1 \leq i \leq \min(n,m)), and n<mn \lt m.

Constraints

  • SS is a string of length between 11 and 2×1052 \times 10^5 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

SS

Output

Print the lexicographically smallest string SS' obtained by permuting the characters in SS.

Sample Input 1

aba

Sample Output 1

aab

Three strings can be obtained by permuting aba:

  • aba
  • aab
  • baa

The lexicographically smallest among them is aab.

Sample Input 2

zzzz

Sample Output 2

zzzz

update @ 2024/3/10 10:25:00