Special binary strings are binary strings with the following two properties:

The number of 0’s is equal to the number of 1’s.

Every prefix of the binary string has at least as many 1’s as 0’s.

Given a special string S, a move consists of choosing two consecutive, non-empty, special substrings of S, and swapping them. (Two strings are consecutive if the last character of the first string is exactly one index before the first character of the second string.)

At the end of any number of moves, what is the lexicographically largest resulting string possible?

Example 1:

1
2
3
4
5
Input: S = "11011000"
Output: "11100100"
Explanation:
The strings "10" [occuring at S[1]] and "1100" [at S[3]] are swapped.
This is the lexicographically largest string possible after some number of swaps.

Note:

  1. S has length at most 50.
  2. S is guaranteed to be a special binary string as defined above.

这道题用暴力就可以过,我在比赛时还尝试了DP,分治……每次迭代都遍历一次字符串,找到所有能交换的Special string,交换它们,从所有交换得到的结果中选择字典序最大的,进入下一个迭代。终止条件是找不到更大的交换后字符串。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution {
public:
string makeLargestSpecial(string S) {
int len = S.length();
string ans = S;
do {
S = ans;
for (int i = 0; i < len; i++) {
for (int j = 1; j + i <= len; j++) {
string tmp = S.substr(i, j);
if (!isSpecial(tmp))
continue;

int cnt = 0;
string maxStr = tmp;
for (int k = 0; k < tmp.length(); k++) {
if (tmp[k] == '0')
cnt--;
else
cnt++;

if (k != tmp.length() - 1 && cnt == 0) {
string a = tmp.substr(0, k + 1), b = tmp.substr(k + 1);
maxStr = max(maxStr, max(a + b, b + a));
}
}

ans = max(ans, S.substr(0, i).append(maxStr).append(S.substr(i + j)));
}
}
} while (ans != S);

return ans;
}

bool isSpecial (const string& str) {
int cnt = 0;
for (int i = 0; i < str.length(); i++) {
if (str[i] == '0')
cnt--;
else
cnt++;
if (cnt < 0)
return false;
}
return cnt == 0;
}
};