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

题意:给你一串由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 思维的更多相关文章

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

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

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

    http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...

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

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

  4. CodeForces - 1009B Minimum Ternary String

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

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

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

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

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

  7. Balanced Ternary String(贪心+思维)

    题目链接:Balanced Ternary String 题目大意:给一个字符串,这个字符串只由0,1,2构成,然后让替换字符,使得在替换字符次数最少的前提下,使新获得的字符串中0,1,2 这三个字符 ...

  8. 牛客多校第四场 A Ternary String

    题目描述 A ternary string is a sequence of digits, where each digit is either 0, 1, or 2. Chiaki has a t ...

  9. 2018牛客网暑期ACM多校训练营(第四场) A - Ternary String - [欧拉降幂公式][扩展欧拉定理]

    题目链接:https://www.nowcoder.com/acm/contest/142/A 题目描述 A ternary string is a sequence of digits, where ...

随机推荐

  1. Python开发异步任务Celery的使用教程!

    1. 生产者消费者设计模式 最常用的解耦方式之一,寻找中间人(broker)搭桥,保证两个业务没有直接关联.我们称这一解耦方式为:生产者消费者设计模式 2.中间人broker 示例:此处演示Redis ...

  2. Java课堂 动手动脑6

    一.下列语句哪一个将引起编译错误?为什么?哪一个会引起运行时错误?为什么? m=d;d=m;d=(Dog)m;d=c;c=(Cat)m; 先进行自我判断, 1.代码: class Mammal{} c ...

  3. sqoop 密码别名模式 --password-alias

    sqoop要使用别名模式隐藏密码 1.首先使用命令创建别名 hadoop credential create xiaopengfei  -provider jceks://hdfs/user/pass ...

  4. IOS7.0唯一“设备ID”的获取方法

    ios7.0 以后通过sysctl获得的mac地址已经失效,所有设备均为020000000000. 可以通过苹果的keychain机制,实现设备的唯一ID标示. 具体过程:在app第一次安装时,生成一 ...

  5. 利用tensorboard可视化checkpoint模型文件参数分布

    写在前面: 上周微调一个文本检测模型seglink,将特征提取层进行冻结,只训练分类回归层,然而查看tensorboard发现里面有histogram显示模型各个参数分布,看了目前这个训练模型参数分布 ...

  6. java线程池,阿里为什么不允许使用Executors?

    带着问题 阿里Java代码规范为什么不允许使用Executors快速创建线程池? 下面的代码输出是什么? ThreadPoolExecutor executor = new ThreadPoolExe ...

  7. 认识Redies

    既然是作为了解性文章,那必然不会做很深入的解读.深入的解读以后会加上. 我们先来回答两个问题.通过这两个问题来开始我们的Redies入门之旅. Redies是什么? Redies有什么作用? Redi ...

  8. 正则表达式之Matcher类中group方法

    前言 同事把一个excel表给我,里面的数据大概有几千的样子吧.自己需要把里面的数据一个一个拿出来做一个http请求,对得到的结果进行过滤,然后再写到上面去.这是就涉及到用脚本来进行操作了,于是自己搞 ...

  9. IntelliJ IDEA 激活(最新)

    注:此文以 Mac 为例,Windows 的激活方法也大同小异.如果不差钱的话,建议购买正版! 1.下载安装 直接通过下面的链接到官网下载最新的 Ultimate 版本即可: https://www. ...

  10. 重学计算机组成原理(十一)- 门电路的"千里传音"

    人用纸和笔来做运算,都是用十进制,直接用十进制和我们最熟悉的符号不是最简单么? 为什么计算机里我们最终要选择二进制呢? 来看看,计算机在硬件层面究竟是怎么表示二进制的,你就会明白,为什么计算机会选择二 ...