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

Posted buerdepepeqi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 (1i|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=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;
}
View Code

 

以上是关于codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题的主要内容,如果未能解决你的问题,请参考以下文章

启发式合并CodeForces - 1009F

Codeforces 1009E

CodeForces - 1009D Relatively Prime Graph

Codeforces 1009FDominant Indices

Codeforces 1009G Allowed Letters FMT,二分图,二分图匹配,霍尔定理

Codeforces 1009G Allowed Letters 最大流转最小割 sosdp