1. 程式人生 > >CF1009B Minimum Ternary String 思維

CF1009B Minimum Ternary String 思維

per 輸出 turn == sid pie sam exist cto

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

  

CF1009B Minimum Ternary String 思維