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

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
#include
#include
#include
#include

using namespace std;

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

int main()
{
	string s;
	cin >> s;
	string ans;
	int cnt = 0;
	multisetms;
	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;
}

 

 

 

你可能感兴趣的:(string字符串处理,c++STL学习)