codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题

时间:2023-03-10 03:26:14
codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题

http://codeforces.com/problemset/problem/1009/B

B. Minimum Ternary String
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

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
input
Copy
100210
output
Copy
001120
input
Copy
11222121
output
Copy
11112222
input
Copy
20
output
Copy
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 ;
}