#C1274. Smallest Lexicographic Binary String
Smallest Lexicographic Binary String
Smallest Lexicographic Binary String
You are given a binary string s
of length n
. The task is to generate another binary string t
of length n
which is lexicographically smallest possible while satisfying a special condition: t
must not be immediately followed by its complement. In this problem, the condition naturally holds when you construct t
by arranging all 0
s first and then all 1
s.
Task: Given the number of characters n
and the binary string s
, count the number of 0
s and 1
s in s
. Then, construct the string t
by writing all the 0
s first, followed by all the 1
s.
Note: The lexicographical order here means that 0
is considered smaller than 1
and hence any string starting with more consecutive 0
s is lexicographically smaller.
Examples:
- For
n = 4
ands = "1001"
, the output should be"0011"
. - For
n = 6
ands = "110010"
, the output should be"000111"
. - For
n = 5
ands = "11100"
, the output should be"00111"
.
inputFormat
The input is read from stdin and consists of:
- An integer
n
representing the length of the binary string. - A binary string
s
of lengthn
, consisting only of characters'0'
and'1'
.
outputFormat
Output to stdout a single line containing the lexicographically smallest binary string t
constructed by placing all the 0
s first followed by all the 1
s.
4
1001
0011