Algorithm


B. Minimum Ternary String
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a ternary string (it is a string which consists only of characters '0', '1' and '2').

You can swap any two adjacent (consecutive) characters '0' and '1' (i.e. replace "01" with "10" or vice versa) or any two adjacent (consecutive) characters '1' and '2' (i.e. replace "12" with "21" or vice versa).

For example, for string "010210" we can perform the following moves:

  • "010210 "100210";
  • "010210 "001210";
  • "010210 "010120";
  • "010210 "010201".

Note than you cannot swap "02 "20" and vice versa. You cannot perform any other operations with the given string excluding described above.

You task is to obtain the minimum possible (lexicographically) string by using these swaps arbitrary number of times (possibly, zero).

String a is lexicographically less than string b (if strings a and b have the same length) if there exists some position i (1i|a|1≤�≤|�|, where |s||�| is the length of the string s) such that for every j<i�<� holds aj=bj��=��, and ai<bi��<��.

Input

The first line of the input contains the string s consisting only of characters '0', '1' and '2', its length is between 11 and 105105 (inclusive).

Output

Print a single string — the minimum possible (lexicographically) string you can obtain by using the swaps described above arbitrary number of times (possibly, zero).

Examples
input
Copy
100210
output
Copy
001120
input
Copy
11222121
output
Copy
11112222
input
Copy
20
output
Copy
20

 



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

int const N = 1e5 + 1;
char s[N], sol[N];

int main() {
  scanf("%s", s);
  int len = strlen(s);

  int cnt = 0;
  for(int i = 0; i < len; ++i)
    cnt += s[i] == '1';

  for(int i = 0, j = 0; i < len; ++i)
    if(s[i] != '1')
      sol[j++] = s[i];

  for(int i = 0; i < len; ++i)
    if(s[i] == '2')
      break;
    else if(s[i] != '1')
      putchar('0');

  for(int i = 0; i < cnt; ++i)
    putchar('1');

  bool ok = false;
  for(int i = 0; i < len; ++i) {
    if(s[i] == '2')
      ok = true;
    if(ok && s[i] != '1')
      putchar(s[i]);
  }

  puts("");

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
100210

Output

x
+
cmd
001120
Advertisements

Demonstration


Codeforces Solution-B. Minimum Ternary String-Solution in C, C++, Java, Python, Minimum Ternary String,Codeforces Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+