codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题
http://codeforces.com/problemset/problem/1009/B
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
遇见这个特别恶心的思维题,打教育场的我直接被这题给教育了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(超级恶心的思维题的更多相关文章
- 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 ...
- 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三个字符的字符串,位置并且逻辑相邻的字符可以相互交换位置,就 ...
- Codeforces ~ 1009B ~ Minimum Ternary String (思维)
题意 给你一个只含有0,1,2的字符串,你可以将"01"变为"10","10"变为"01","12" ...
- codeforces round 472(DIV2)D Riverside Curio题解(思维题)
题目传送门:http://codeforces.com/contest/957/problem/D 题意大致是这样的:有一个水池,每天都有一个水位(一个整数).每天都会在这一天的水位上划线(如果这个水 ...
- C. Meaningless Operations Codeforces Global Round 1 异或与运算,思维题
C. Meaningless Operations time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 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 ...
- Balanced Ternary String CodeForces - 1102D (贪心+思维)
You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' ...
随机推荐
- linux特殊权限位suid
特殊权限位基本说明(了解): linux系统基本权限位为9位权限,但还有额外3位权限位,共12位权限: suid s(x) S 4 用户对应的权限位(用户对应的3位 ...
- JavaScript---DOM对象(DHTML)
1.什么是DOM? DOM 是 W3C(万维网联盟)的标准.DOM 定义了访问 HTML 和 XML 文档的标准: "W3C 文档对象模型(DOM)是中立于平台和语言的接口,它允许程序和脚本 ...
- MUI常用脚本及原生JavaScript常用操作元素方法
1.mui元素转换html元素 var obj=mui("#id")[0]; 2.事件绑定 var btn = document.getElementById("logi ...
- SGU 169 numbers 数学
169.Numbers Let us call P(n) - the product of all digits of number n (in decimal notation). For exam ...
- java_servlet
在servlet 解决中文乱码 response.setContentType("text/html;charset=utf-8"); //必须在getWrite()上面,不然依然 ...
- 零基础学css第二天
内边距与外边距: <!DOCTYPE html> <html> <head> <title></title> <style type= ...
- sql中给逗号分隔的查询结果替换单引号
技术交流群:233513714 第一种方法: SELECT * FROM pay_inf_config a WHERE a.id IN ( SELECT REPLACE ( concat('''', ...
- easyui js拼接html,class属性失效的问题
问题:要在前一个按钮之后添加相同的样式的按钮,通过$("#cj").html(str); 这样的形式添加,却不能添加上样式 <div id="btn" c ...
- ElasticSearch学习笔记(五)-- 排序、分页与遍历
1. 相关性算分 这样能够查询到不同分片上的文档的准确算分,默认分片为5 2. sorting-doc-values-fielddata 3. 分页与遍历
- IOS客户端的个人中心可以查看自己的博客了。
IOS客户端的个人中心可以查看自己的博客了. 写这篇是为了在客户端显示之用. 下一步实现在客户端发博客.