CF1009B Minimum Ternary String 思维

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 aa is lexicographically less than string bb (if strings aa and bb have the same length) if there exists some position ii (1i|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
Copy
100210
output
Copy
001120
input
Copy
11222121
output
Copy
11112222
input
Copy
20
output
Copy
20

 

 题意:给你一串由0,1,2组成的字符串,除了0和2不能交换,其他任意字符可以两两交换,问能交换得到的最小字典序字符串是啥?

分析:首先记录所有1的个数,然后再遍历一次数组,记录0的个数,遇到2的时候判断,如果是第一次遇到2则先输出记录的0的个数,然后再输出1的个数,接着输出个2,后面遇到2的时候就不要再输出1(因为1和2可以交换,所以1都会被换到第一次输出去),

最后到结尾的时候输出0

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
typedef long long ll;
ll a[maxn], vis[maxn];
int main() {
    string s;
    while( cin >> s ) {
        ll a = 0, b = 0, c = 0;
        for( ll i = 0; i < s.length(); i ++ ) {
            if( s[i] == '1' ) {
                b ++;
            }
        }
        for( ll i = 0; i < s.length(); i ++ ) {
            if( s[i] == '0' ) {
                a ++;
            } else if( s[i] == '2' ) {
                c ++;
                for( ll j = 0; j < a; j ++ ) {
                    cout << 0;
                }
                if( c == 1 ) {
                    for( ll j = 0; j < b; j ++ ) {
                        cout << 1;
                    }
                }
                cout << 2;
                a = 0, b = 0;
            }
            if( i == s.length()-1 ) {
                for( ll j = 0; j < a; j ++ ) {
                    cout << 0;
                }
                if( c == 0 ) {
                    for( ll j = 0; j < b; j ++ ) {
                        cout << 1;
                    }
                }
            }
        }
        cout << endl;
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/l609929321/p/9314018.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值