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
这场CF abcd题 我就觉得B题最难
这个题目卡了我好久好久 ,全是写BUG
代码有点毒瘤
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + ;
const int INF = 0x3fffffff;
typedef long long LL;
string s;
int sum0[maxn], sum1[maxn], sum2[maxn], vis[maxn];
vector<int>a;
int main() {
cin >> s;
if (s[] == '') sum0[] = ;
if (s[] == '') sum1[] = ;
if (s[] == '') sum2[] = ;
for (int i = ; i < s.size() ; i++) {
if (s[i] == '') sum0[i + ] = sum0[i] + , sum1[i + ] = sum1[i], sum2[i + ] = sum2[i];
if (s[i] == '') sum1[i + ] = sum1[i] + , sum0[i + ] = sum0[i], sum2[i + ] = sum2[i];
if (s[i] == '') sum2[i + ] = sum2[i] + , sum0[i + ] = sum0[i], sum1[i + ] = sum1[i];
}
int cnt = ;
int len = s.size();
for (int i = ; i < len ; i++) {
if (s[i] == '') {
for (int j = ; j < sum0[i]; j++) a.push_back();
for (int j = ; j < sum1[len] ; j++) a.push_back();
for (int j = i ; j < s.size(); j++) {
if (s[j] == '') continue;
if (s[j] == '') a.push_back();
if (s[j] == '')a.push_back();
}
cnt = ;
break;
}
}
if (cnt == ) {
for (int i = ; i < sum0[len] ; i++) printf("");
for (int i = ; i < sum1[len] ; i++) printf("");
} else {
for (int i = ; i < a.size() ; i++) printf("%d", a[i]);
}
return ;
}