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 (1≤i≤|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=;
int num0=,num1=;
for(int i=;i<len;i++){
if(str[i]==''&&flag==){
flag=;
pos=i;
}
if(str[i]=='') num1++;
if(str[i]==''&&flag==) num0++;
}
for(int i=;i<num0;i++){
cout<<"";
}
for(int i=;i<num1;i++){
cout<<"";
}
if(flag==){
for(int i=pos;i<len;i++){
if(str[i]=='') continue;
cout<<str[i];
}
}
cout<<endl;
return ;
}

codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题的更多相关文章

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

    B. Minimum Ternary String time limit per test 1 second memory limit per test 256 megabytes input sta ...

  2. CF1009B Minimum Ternary String 思维

    Minimum Ternary String time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. CodeForces - 1009B Minimum Ternary String

    You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). ...

  4. Educational Codeforces Round 47 (Rated for Div. 2) :B. Minimum Ternary String

    题目链接:http://codeforces.com/contest/1009/problem/B 解题心得: 题意就是给你一个只包含012三个字符的字符串,位置并且逻辑相邻的字符可以相互交换位置,就 ...

  5. Codeforces ~ 1009B ~ Minimum Ternary String (思维)

    题意 给你一个只含有0,1,2的字符串,你可以将"01"变为"10","10"变为"01","12" ...

  6. codeforces round 472(DIV2)D Riverside Curio题解(思维题)

    题目传送门:http://codeforces.com/contest/957/problem/D 题意大致是这样的:有一个水池,每天都有一个水位(一个整数).每天都会在这一天的水位上划线(如果这个水 ...

  7. C. Meaningless Operations Codeforces Global Round 1 异或与运算,思维题

    C. Meaningless Operations time limit per test 1 second memory limit per test 256 megabytes input sta ...

  8. Codeforces Round #280 (Div. 2) E. Vanya and Field 思维题

    E. Vanya and Field time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. Balanced Ternary String CodeForces - 1102D (贪心+思维)

    You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' ...

随机推荐

  1. 一、小程序连接MySql数据库

    前言:我用的是wafer2 node.解决方案 下面连接有环境配置及搭建流程(https://github.com/tencentyun/wafer2-quickstart-nodejs) ,连接是官 ...

  2. mybatis的坑——不报错,就是不能committing,数据存不进数据库

    测试的时候会报空指针异常,在项目跑的时候会停止执行程序,不会出现异常. 经过一星期的排查与测试,最终找到错误,把mapper文件的映射属性名写错了. property属性名要与接收类的属性名保持一致. ...

  3. 干货!一篇文章集合所有Linux基础命令,适合所有菜鸟学习和老手回顾!

    1 文件{ ls -rtl # 按时间倒叙列出所有目录和文件 ll -rt touch file # 创建空白文件 rm -rf 目录名 # 不提示删除非空目录(-r:递归删除 -f强制) dos2u ...

  4. phpmailer类的再封装

    email <?php use PHPMailer\PHPMailer\PHPMailer; class Email { const SMTPDebug = 2; const HOST = 's ...

  5. python--随笔一

    1.format函数--根据关键字和位置选择性插入数据 In [11]: '{mingzi}jintian{dongzuo}'.format(mingzi='duzi',dongzuo='i love ...

  6. 【Effective C++读书笔记】序

    C++ 是一个难学易用的语言! [C++为什么难学?] C++的难学,不仅在其广博的语法,以及语法背后的语义,以及语义背后的深层思维,以及深层思维背后的对象模型: C++的难学还在于它提供了四种不同而 ...

  7. 【转载】Callable、FutureTask中阻塞超时返回的坑点

    本文转载自:http://www.cnblogs.com/starcrm/p/5010863.html 案例1: package com.net.thread.future; import java. ...

  8. c++ tie,ignore

    ignore 一个未指定的类型对象,任何值都可以没有影响地赋值给它.通常使用tie来解压一个元组,作为可以忽略的占位符. #include <iostream> #include < ...

  9. ZOJ3329 概率DP

    One Person Game Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge There is a very ...

  10. spark的排序方法

    今天我们来介绍spark中排序的操作,spark的排序很简单,我们可以直接使用sortBy来进行,这个里面我们使用case clas,使用case class的好处是1.不用newjiukeyi 搞出 ...