1. 程式人生 > >B. Minimum Ternary String(字串的處理)

B. Minimum Ternary String(字串的處理)

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

 

題意:1可以任意移動,但2卻只能和1交換。求值最小的串。

解法:如果要使值最小,那麼儘可能的使所有0在最前面,1在第一個2的前面時,值最小。   所以將所有的1移到第一個2的前面時值最小。

           還有幾種特殊情況:  1.當字串中沒有2時,將所有的1移到0的後面,即先輸出所有的0,在輸出所有的1.

                                             2.當字串中只有1時,直接輸出所有的1即可。

 

STL::string的相關知識:在這裡用到了字串的拼接,插入操作。  

                                       拼接用到的是 ‘+’加號,它可以使兩個字串拼接在一塊。

                                      插入操作用到insert()函式。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<set>

using namespace std;

const int maxn = 1e5+5;
int pos[maxn];

int main()
{
	string s;
	cin >> s;
	string ans;
	int cnt = 0;
	multiset<int>ms;
	for(int i = 0; i < s.length(); i++){
		if(s[i] == '1') cnt++;
		else{
			ans += s[i];
			ms.insert(s[i]);	
		} 
	}
	
	if(ans.length() == 0){
		for(int i = 0; i < cnt; i++) cout << '1';
		return 0;
	}
	
	if(ms.count('2') == 0){
		cout << ans;
		for(int i = 0; i < cnt; i++) cout << '1';		
		return 0;
	}
	
	int pos;
	for(int i = 0; i < ans.length(); i++){
		if(ans[i] == '2'){
			pos = i;
			break;
		}
	}
	ans = ans.insert(pos, string(cnt, '1')); 
	cout << ans << endl;
}