zoukankan      html  css  js  c++  java
  • codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题

    http://codeforces.com/problemset/problem/1009/B

    B. 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

     遇见这个特别恶心的思维题,打教育场的我直接被这题给教育了QAQ

    题意:给你一个由0 1 2 构成的字符串,其中0可以和1、1可以和2交换位置,求交换后字典序最小的的串

    题解:我们来冷静分析一波,1可以和0换,可以和1换,那么1在这个字符串当中就可以任意滑动,要想字符串的字典序最小

    那么所有的1肯定会在第一个出现2前面,要是没有2,那么最后的字符串一定会是000111的形式,如果有2的话,第一个2后面1一定全部在第一个2的前面

    第一个二后面的0和2实际上就不会变化.

    代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    int main(){
        string str;
        cin>>str;
        int len=str.size();
        int pos;
        int flag=0;
        int num0=0,num1=0;
        for(int i=0;i<len;i++){
          if(str[i]=='2'&&flag==0){
            flag=1;
            pos=i;
          }
          if(str[i]=='1') num1++;
          if(str[i]=='0'&&flag==0) num0++;
        }
        for(int i=0;i<num0;i++){
          cout<<"0";
        }
        for(int i=0;i<num1;i++){
          cout<<"1";
        }
        if(flag==1){
          for(int i=pos;i<len;i++){
            if(str[i]=='1') continue;
            cout<<str[i];
          }
        }
        cout<<endl;
        return 0;
    }
    View Code
    每一个不曾刷题的日子 都是对生命的辜负 从弱小到强大,需要一段时间的沉淀,就是现在了 ~buerdepepeqi
  • 相关阅读:
    e家modem共享上网方法
    千里奔丧
    解决dbvisualizer乱码问题Ubuntu手记之软件
    目录结构Ubuntu手记之系统配置
    javaFTP编程
    JAVA运行环境设置
    VPNCUbuntu手记之软件
    清洗节气门
    IPMSGUbuntu手记之软件
    AIX下的JAVA线程监视
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/9311722.html
Copyright © 2011-2022 走看看