Minimum Ternary String (CodeForces - 1009B)

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 aa is lexicographically less than string bb (if strings aa and bb have the same length) if there exists some position ii (1≤i≤|a|1≤i≤|a|, where |s||s| is the length of the string ss) such that for every j<ij<i holds aj=bjaj=bj, and ai<biai<bi.

Input

The first line of the input contains the string ss 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

100210

Output

001120

Input

11222121

Output

11112222

Input

20

Output

20

题解:观察交换规则我们发现,01可以交换,12可以交换,那么1字符作为一个承上启下的字符,他可以在整个字符串中任意滑动,因此字符串中的所有的1必须都连续的摆放在第一个2的前面,而第一个2前面的0毫无疑问连续的放在最前面,而第一个2后面的0,因为被2挡住,无论如何都不可能越过前面的2,故从第一个2开始后面的0和2相对顺序不变,因此我们只需要统计出第一个2前面的0有多少个,所有1有多少个,第一个2的位置,输出时,先输出第一个2前面的所有0,再输出所有1,然后从第一个2的位置开始往后遍历只输出0和2.

代码如下:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<algorithm>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define maxn 320007
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1000000009
#define e  2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
using namespace std;

char a[1111111];
int main()
{
    cin>>a;
    int flag=0;
    int pos;
    ll num1=0;
    ll num0=0;
    int l=strlen(a);
    for(int i=0;i<l;i++)
    {
        if(flag==0&&a[i]=='2')
        {
            flag=1;
            pos=i;
        }
        if(a[i]=='1')
            num1++;
        if(flag==0&&a[i]=='0')
            num0++;
    }
    for(int i=0;i<num0;i++)
    {
        cout<<"0";
    }
    for(int i=0;i<num1;i++)
    {
        cout<<"1";
    }
    for(int i=pos;i<l;i++)
    {
        if(a[i]=='1')
            continue;
        cout<<a[i];
    }
    cout<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值