B. Minimum Ternary String

链接 :
Problem - 1009B - Codeforces
思路 :
10 , 12 可以互相交换,就代表着1可以出现在任何地方,要追求字典序最小,那么应该将所有的1放在哪里呢 ?
应该放在第一个2前面,要注意的是 : 没有2的情况;
代码 :
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
using namespace std;
typedef long long LL;
const int N = 2e5+10;
inline void solve(){
	string s ; cin >> s;
	bool tag = false;
	string ans = "";
	int t = 0 , idx = N , n = s.size();
	for(int i=0;i<n;i++){
		if(s[i]=='1') t ++;
		else if(s[i]=='2'){
			tag = true;
			idx = min(idx , i);
		}
		else continue;
	}
	if(tag){
		for(int i=0;i<idx;i++){
			if(s[i]=='0') ans += s[i];
		}
		for(int i=0;i<t;i++) ans += '1';
		for(int i=idx;i<n;i++){
			if(s[i] != '1') ans += s[i];
		}
	}else {
		for(int i=0;i<n-t;i++) ans += '0';
		for(int i=0;i<t;i++) ans += '1';
	}
	cout << ans << endl;
}
 
int main()
{
    IOS
    int _ = 1;
    // cin >> _;
    while(_ --) solve();
    return 0;
} 
                


















