zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

    彼时当年少,莫负好时光。
  • 相关阅读:
    在win8.1 64位环境下有关Oracle的安装和卸载
    动手又动脑
    二柱子四则运算(课堂练习)
    Java学生成绩绩点管理系统
    小学期第八周收获
    小学期第七周收获
    小学期第六周收获
    小学期第五周收获
    《大道至简》读后感
    小学期第四周收获
  • 原文地址:https://www.cnblogs.com/l609929321/p/9314018.html
Copyright © 2011-2022 走看看