CF1009B Minimum Ternary String 思维
1 second
256 megabytes
standard input
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.
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).
Print a single string — the minimum possible (lexicographically) string you can obtain by using the swaps described above arbitrary number of times (possibly, zero).
100210
001120
11222121
11112222
20
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 思维的更多相关文章
- Codeforces ~ 1009B ~ Minimum Ternary String (思维)
题意 给你一个只含有0,1,2的字符串,你可以将"01"变为"10","10"变为"01","12" ...
- codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题
http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...
- B. Minimum Ternary String (这个B有点狠)
B. Minimum Ternary String time limit per test 1 second memory limit per test 256 megabytes input sta ...
- CodeForces - 1009B Minimum Ternary String
You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). ...
- Educational Codeforces Round 47 (Rated for Div. 2) :B. Minimum Ternary String
题目链接:http://codeforces.com/contest/1009/problem/B 解题心得: 题意就是给你一个只包含012三个字符的字符串,位置并且逻辑相邻的字符可以相互交换位置,就 ...
- Balanced Ternary String CodeForces - 1102D (贪心+思维)
You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' ...
- Balanced Ternary String(贪心+思维)
题目链接:Balanced Ternary String 题目大意:给一个字符串,这个字符串只由0,1,2构成,然后让替换字符,使得在替换字符次数最少的前提下,使新获得的字符串中0,1,2 这三个字符 ...
- 牛客多校第四场 A Ternary String
题目描述 A ternary string is a sequence of digits, where each digit is either 0, 1, or 2. Chiaki has a t ...
- 2018牛客网暑期ACM多校训练营(第四场) A - Ternary String - [欧拉降幂公式][扩展欧拉定理]
题目链接:https://www.nowcoder.com/acm/contest/142/A 题目描述 A ternary string is a sequence of digits, where ...
随机推荐
- 提交bug的标准及书写规范
Bug有效性 1.交付过程中测试者需按照专家设定好的模块,对Bug进行归类提交: 2.Bug的类型默认为UI问题.功能问题.崩溃问题,提交Bug时不能弄错: 3.需求是否明确.前提条件是否满足.输入数 ...
- 使用vue实现行列转换的一种方法。
行列转换是一个老生常谈的问题,这几天逛知乎有遇到了这个问题.一个前端说,拿到的数据是单列的需要做转换才能够绑定,折腾了好久才搞定,还说这个应该后端直接出数据,不应该让前端折腾. 这个嘛,行列转换在后端 ...
- mac 下 docker 镜像加速器
配置镜像加速器 具体设置见下图即可. 
- jq css3实现跑马灯+大转盘
前端效果, <!DOCTYPE HTML><html><head> <meta http-equiv="Content-Type" con ...
- APP启动优化
1. 去除启动黑屏 1.1 在style.xml中定义两种主题: <style name="AppTheme" parent="Theme.AppCompat.Da ...
- [原创实践]RedHat Enterprise Linux 5 安装GCC和redis
Redis的安装需要使用GCC,Red Hat Enterprise 5默认是不安装gcc的,需要自己手动安装. 1:查看系统中是否有gcc gcc -v 查看本机linux版本 lsb_releas ...
- Json串与实体的相互转换 (不依赖于jar包 只需Eclipse环境即可)
Json串与实体的相互转换 (不依赖于jar包 只需Eclipse环境即可) 最近学习了javaWeb开发,用的是ssh框架里面自己整合了hibernate 和Struts2 和spring框架,其中 ...
- 提取html内的文字1
public static string StripHTML(string strHtml) { string [] aryReg ={ @"<scrip ...
- spring事务在实际项目开发中的使用
一, 事务的一些基础知识简单回顾一下,讲的不是很深入,网上博客很多. 1,关于事务的四大特性:原子性.隔离性.一致性.隔离性 本文不再赘述: 2,事务的隔离级别:读未提交,读已提交,可重复读,串行 ...
- 行车记+翻车记:.NET Core 新车改造,C# 节能降耗,docker swarm 重回赛道
非常抱歉,10:00~10:30 左右博客站点出现故障,给您带来麻烦了,请您谅解. 故障原因与博文中谈到的部署变更有关,但背后的问题变得非常复杂,复杂到我们都在怀疑与阿里云服务器 CPU 特性有关. ...