B. Minimum Ternary String(字符串的处理)

博客围绕三元字符串展开,可交换相邻的'0'与'1'、'1'与'2',目标是得到字典序最小的字符串。解法是让所有0在最前,1在第一个2前。还提及特殊情况处理。同时介绍了STL::string的拼接和插入操作,拼接用'+',插入用insert()函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值