|
| 1 | +# [761.特殊的二进制序列](https://leetcode.cn/problems/special-binary-string/) |
| 2 | + |
| 3 | +<p>特殊的二进制序列是具有以下两个性质的二进制序列:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>0 的数量与 1 的数量相等。</li> |
| 7 | + <li>二进制序列的每一个前缀码中 1 的数量要大于等于 0 的数量。</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p>给定一个特殊的二进制序列 <code>S</code>,以字符串形式表示。定义一个<em>操作 </em>为首先选择 <code>S</code> 的两个连续且非空的特殊的子串,然后将它们交换。(两个子串为连续的当且仅当��一个子串的最后一个字符恰好为第二个子串的第一个字符的前一个字符。)</p> |
| 11 | + |
| 12 | +<p>在任意次数的操作之后,交换后的字符串按照字典序排列的最大的结果是什么?</p> |
| 13 | + |
| 14 | +<p><strong>示例 1:</strong></p> |
| 15 | + |
| 16 | +<pre> |
| 17 | +<strong>输入:</strong> S = "11011000" |
| 18 | +<strong>输出:</strong> "11100100" |
| 19 | +<strong>解释:</strong> |
| 20 | +将子串 "10" (在S[1]出现) 和 "1100" (在S[3]出现)进行交换。 |
| 21 | +这是在进行若干次操作后按字典序排列最大的结果。 |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p><strong>说明:</strong></p> |
| 25 | + |
| 26 | +<ol> |
| 27 | + <li><code>S</code> 的长度不超过 <code>50</code>。</li> |
| 28 | + <li><code>S</code> 保证为一个满足上述定义的<em>特殊 </em>的二进制序列。</li> |
| 29 | +</ol> |
| 30 | + |
| 31 | +<details> |
| 32 | +<summary>标签:</summary> |
| 33 | +['递归', '字符串'] |
| 34 | +</details> |
| 35 | + |
| 36 | +<details> |
| 37 | +<summary>难度:Hard</summary> |
| 38 | +喜欢:176 |
| 39 | +</details> |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +# 算法 1 |
| 44 | + |
| 45 | +## 括号序列 + 递归 $O(n^2logn)$ |
| 46 | + |
| 47 | +### 算法思路 |
| 48 | + |
| 49 | +1. 先将字符串划分成同级括号序列 |
| 50 | +2. 将同级括号序列进行排序,得到字典序最小的序列 |
| 51 | +3. 递归上述过程 |
| 52 | + |
| 53 | +### 前置知识 |
| 54 | + |
| 55 | +括号序列:[剑指 Offer II 085. 生成匹配的括号](https://leetcode.cn/problems/IDBivT/) |
| 56 | + |
| 57 | +括号序列排序: [剑指 Offer 45. 把数组排成最小的数](https://leetcode.cn/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/) |
| 58 | + |
| 59 | +## 时间复杂度 |
| 60 | + |
| 61 | +递归分治分成 $n$ 层 |
| 62 | + |
| 63 | +每层排序 $nlogn$ |
| 64 | + |
| 65 | +总时间复杂度:$O(n^2logn)$ |
| 66 | + |
| 67 | +## 代码实现 |
| 68 | + |
| 69 | +```java [] |
| 70 | + |
| 71 | +``` |
| 72 | + |
| 73 | +```cpp [] |
| 74 | +class Solution { |
| 75 | +public: |
| 76 | + string makeLargestSpecial(string S) { |
| 77 | + if (S.size() <=2) return S; |
| 78 | + vector<string> q; // 记录 同级括号,用来排序 |
| 79 | + string s; |
| 80 | + int cnt = 0 ; // 判断 同级括号 |
| 81 | + for (char c: S) { |
| 82 | + s+=c; |
| 83 | + if (c=='1') cnt++; |
| 84 | + else { |
| 85 | + cnt--; |
| 86 | + if (cnt == 0) { |
| 87 | + q.push_back('1' + makeLargestSpecial(s.substr(1, s.size()-2)) + '0'); |
| 88 | + s.clear(); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + sort(q.begin(), q.end(), [](string&a, string&b){ // 同级括号 字典序最小 |
| 93 | + return a + b > b+a; |
| 94 | + }); |
| 95 | + string res ; |
| 96 | + for (auto x: q) res += x; |
| 97 | + return res; |
| 98 | + } |
| 99 | +}; |
| 100 | +``` |
| 101 | +
|
| 102 | +## 参考文献 |
| 103 | +
|
| 104 | +# 相关题目 |
| 105 | +
|
| 106 | +## [AcWing 889. 满足条件的 01 序列](https://www.acwing.com/problem/content/891/) |
| 107 | +
|
| 108 | +## [剑指 Offer II 085. 生成匹配的括号](https://leetcode.cn/problems/IDBivT/) |
| 109 | +
|
| 110 | +## [剑指 Offer 45. 把数组排成最小的数](https://leetcode.cn/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/) |
0 commit comments