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 ...
随机推荐
- [转帖]升级 Ubuntu,解决登录时提示有软件包可以更新的问题
升级 Ubuntu,解决登录时提示有软件包可以更新的问题 2017年12月05日 11:58:17 阅读数:2953更多 个人分类: ubuntu Connecting to ... Connecti ...
- .NET Socket 简单入门
说到Socket,想必大家都或多或少有所涉及,从最初的计算机网络课程,讲述了tcp协议,而Socket就是对协议的进一步封装,使我们开发人员能够更加容易轻松的进行软件之间的通信. 这个星期刚好接受一个 ...
- Qss 样式表的尝试
QLineEdit{ border:1px solid #137eb6; padding:2px; background-color:#F5F5F5; } QToolTip{ border:1px s ...
- BZOJ5118 Fib数列2(矩阵快速幂)
特殊矩阵的幂同样满足费马小定理. #include<iostream> #include<cstdio> #include<cmath> #include<c ...
- CodeForces - 988D(思维STL)
原文地址:https://blog.csdn.net/weixin_39453270/article/details/80548442 博主已经讲的很好了 题意: 从一个序列中,选出一个集合,使得集合 ...
- uoj35 后缀排序
题目链接:http://uoj.ac/problem/35 这是一道模板题. 读入一个长度为 n 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第 ...
- WEB入门.七 CSS布局模型
学习内容 标准文档流 流动模型(flow model) 浮动模型(float model) CSS基本布局 能力目标 理解标准文档流 使用流动模型实现页面布局 使用浮动模型实现页面布局 掌握常用CSS ...
- java Class.getSimpleName() 的用法
Usage in android: private static final String TAG = DemoApplication.class.getSimpleName(); public cl ...
- 2018.9.20 Educational Codeforces Round 51
蒟蒻就切了四道水题,然后EF看着可写然而并不会,中间还WA了一次,我太菜了.jpg =.= A.Vasya And Password 一开始看着有点虚没敢立刻写,后来写完第二题发现可以暴力讨论,因为保 ...
- bzoj 3611: [Heoi2014]大工程 && bzoj 2286: [Sdoi2011消耗战
放波建虚树的模板. 大概是用一个栈维护根节点到当前关键点的一条链,把其他深度大于lca的都弹出去. 每次做完记得复原. 还有sort的时候一定要加cmp!!! bzoj 3611 #include&l ...