CodeForces - 1009B Minimum Ternary String
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
100210
001120
11222121
11112222
20
20 开始时想的过于狭隘了,局部分析未果的情况下,没有做到整体的去分析。 对 0 1 2 的特点进行分析,1 既可以与 0 交换 ,也可以与 2 交换 ,所以说 1 可以出现在字符串的任意位置,为了使字符串的字典序小,1 必定要出现在 2 的前面,且要出现在第一个 2 的前面,所以 1 的位置就确定了。
再看 0 2 ,因为所有的 1 已经都被抽到了第一个 2 的前面,且 2 不能借助 1 使它本身换到 0 的后面(以 2 1 0为例),所以,第一个 2 后面的序列是不变的。
再看第一个 2 前面的序列,显然,第一个 2 之前的 0 要在所有的 1 之前。
分析完毕。 切入点:0 1 2 的特点、字符串的格式
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
const double e=exp();
const int N = ; char con[];
int main()
{
int i,p,j,n,k;
int head,tail,a=,b=,flag; scanf("%s",con);
k=strlen(con);
for(i=;i<k;i++)
{
if(con[i]=='')
a++;
}
flag=;
for(i=;i<k;i++)
{
if(con[i]=='')
printf("");
else if(con[i]==''&&flag==)
{
for(j=;j<=a;j++)
printf("");
printf("");
flag=;
}
else if(con[i]==''&&flag==)
{
printf("");
}
}
if(flag==)
{
for(i=;i<=a;i++)
printf("");
}
return ;
}
CodeForces - 1009B 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 ...
- CF1009B Minimum Ternary String 思维
Minimum Ternary String time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 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' ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- 牛客多校第四场 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 ...
随机推荐
- Delphi CreateMutex 防止程序多次运行
windows是个多用户多任务的操作系统,支持多个程序同时运行,如果你的程序不想让用户同时运行一个以上, 那应该怎样做呢? 本文将介绍避免用户同时运行多个程序的例子. 需要用到的函数CreateMut ...
- 面试问题总结二(技术能力-PHP)----Ⅲ
42.什么是单点登录? 答:单点登录 SSO(Single Sign On)说得简单点就是在一个多系统共存的环境下,用户在一处登录后,就不用在其他系统中登录,也就是用户的一次登录能得到其他所有系统的信 ...
- Linux_Nginx 安装
官网:http://nginx.org/ 1.下载http://nginx.org/download/nginx-1.14.0.tar.gz 2.查看详情 [zwesy@localhost ~]$ l ...
- [学习]仿照cnblog 搭建 Oracle RAC 双节点 困.. 后续做不下去了..
1. 学习地址: https://blog.csdn.net/yuzifen?t=1 2. 克隆之前创建好的虚拟机: 3. 给虚拟机增加新的硬盘. 注意事项 1) 必须是厚置备的磁盘才可以. 2) 必 ...
- 浅谈JavaScript预编译原理
这两天又把js的基础重新复习了一下,很多不懂得还是得回归基础,大家都知道js是解释性语言,就是编译一行执行一行,但是在执行的之前,系统会做一些工作: 1,语法分析: 2,预编译: 3,解释执行. 语法 ...
- Lattice Point or Not UVA - 11768(拓展欧几里得)
原文地址:https://www.cnblogs.com/zyb993963526/p/6783532.html 题意: 给定两个点A(x1,y1)和B(x2,y2),均为0.1的整数倍.统计选段AB ...
- Prime k-tuple UVA - 1404
就是大区间求素数 参考 LightOJ - 1197 https://www.cnblogs.com/WTSRUVF/p/9190660.html 直接套那个代码就好了 #include <i ...
- GIT 旧库迁移到新库
1.在gitlab创建新项目,得到SSH地址2.用gitextent打开旧项目,记得所有分支合并成一个(如果确实无法合并,则需要一个个复位推送)3.复位到需要推送的节点分支4.打开菜单栏的档案库,管理 ...
- hdu5909 Tree Cutting 【树形dp + FWT】
题目链接 hdu5909 题解 设\(f[i][j]\)表示以\(i\)为根的子树,\(i\)一定取,剩余节点必须联通,异或和为\(j\)的方案数 初始化\(f[i][val[i]] = 1\) 枚举 ...
- Android Studio快捷键设置之实现原eclipse中ctrl+m的全屏的效果
如下图,keymap设置成eclipse的的,但是鼠标双击最大化复原没有,ctr+M全屏也没有, 那么久在如下图的三个项目上添加鼠标双击和快捷键---自己方便好用,但不冲突的 Toggle full ...