CF1009B Minimum Ternary String 思维

Posted l609929321

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

  

以上是关于CF1009B Minimum Ternary String 思维的主要内容,如果未能解决你的问题,请参考以下文章

题解 CF1354B Ternary String

题解 CF1354B Ternary String

B. Minimum Ternary String (这个B有点狠)

codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题

CodeForces 1009B(思路)

CF804B Minimum number of steps