http://codeforces.com/problemset/problem/1009/B
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=0; int num0=0,num1=0; for(int i=0;i<len;i++){ if(str[i]=='2'&&flag==0){ flag=1; pos=i; } if(str[i]=='1') num1++; if(str[i]=='0'&&flag==0) num0++; } for(int i=0;i<num0;i++){ cout<<"0"; } for(int i=0;i<num1;i++){ cout<<"1"; } if(flag==1){ for(int i=pos;i<len;i++){ if(str[i]=='1') continue; cout<<str[i]; } } cout<<endl; return 0; }