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. 交换机基础配置之三层交换机实现vlan间通信

    我们以上面的拓扑图做实验,要求为pc1,pc2,pc3配置为vlan10,pc4,pc5,pc6配置为vlan20,pc7,pc8,pc9配置为vlan30 server0和server1配置为vla ...

  2. Python学习笔记:PEP8常用编程规范

    PEP8编码规范是一种非常优秀的编码规范,也得到了Python程序员的普遍认可,如果实践中或者项目中没有统一的编码规范,建议尽量遵循PEP8编码规范,当然如果项目中已经有了自身的编码规范,应当优先遵循 ...

  3. scrapy如何实现分布式爬虫

    使用scrapy爬虫的时候,记录一下如何分布式爬虫问题: 关键在于多台主机协作的关键:共享爬虫队列 主机:维护爬取队列从机:负责数据抓取,数据处理,数据存储 队列如何维护:Redis队列Redis 非 ...

  4. java线程安全(单例模式)(转载)

    原文链接:http://www.jameswxx.com/java/%E8%AF%B4%E8%AF%B4%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F/ 单例模式?多么简单! ...

  5. Managing WMI security

    TechNet Library TechNet Archive Windows Server 2003 Product Help Windows Server 2003 Product Help Ad ...

  6. php导出数据为CSV文件DEMO

    代码示例: private function _download_send_headers($filename) { // disable caching $now = gmdate("D, ...

  7. 【Invert Binary Tree】cpp

    题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...

  8. Intellij 设置只更新静态文件(js、view、css)的方法

    1.打开 Tomcat Run/Debug configuration 2.打开Deployment标签 3.在“Deploy at Server Startup” 中,移出现有的.war 包 4.点 ...

  9. 选择MariaDB的压缩数据引擎TokuDB

    业务运用场景 数据基本不用update, 不频繁的范围查询 数据存储量较大(为以后准备) 选择占用磁盘较小的db 业务对数据库插入操作频繁,为避免影响其它业务,需要将直播业务的DB 独立出来,选择另外 ...

  10. 原始套接字--简易ping程序

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> ...