#P9458. [入门赛 #14] 扶苏和串 (Hard Version)

[入门赛 #14] 扶苏和串 (Hard Version)

Background

As everyone knows, the string problems in the monthly Beginner Contest are all ideas enumerated by Fusu.

Problem Description

Given a 01 string ss, you may choose any non-empty substring of ss and reverse this substring once within ss.

What is the lexicographically smallest string you can obtain?

Formally, you can choose an interval [l,r][l, r] satisfying 1lrs1 \leq l \leq r \leq |s|, and construct a string tt such that:

$$t_i = \begin{cases}s_i, &i < l \text{ or } i > r \\ s_{r - (i - l)}, & l \leq i \leq r\end{cases}$$

Minimize the lexicographic order of string tt.

Input Format

The input contains only one line with a string, representing ss.

Output Format

Output one line with a string, representing the lexicographically smallest string that can be obtained.

101
011
0010100
0000101

Hint

Explanation for Sample 1

s=101s = \texttt{\underline{10}1}. Reverse the underlined substring to get t=011t = \texttt{011}.

Explanation for Sample 2

s=0010100s = \texttt{00\underline{10100}}. Reverse the underlined substring to get 0000101\texttt{0000101}.

Constraints

Let s|s| denote the length of the input string.

  • For 100%100\% of the testdata, 1s30001 \leq |s| \leq 3000. ss contains only characters 0,1\texttt{0,1}.

Translated by ChatGPT 5