A16052 | Best Binary String
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a string $s$ consisting of the characters 0, 1 and/or ?. Let's call it a pattern.
Let's say that the binary string (a string where each character is either 0 or 1) matches the pattern if you can replace each character ? with 0 or 1 (for each character, the choice is independent) so that the strings become equal. For example, 0010 matches ?01?, but 010 doesn't match 1??, ??, or ????.
Let's define the cost of the binary string as the minimum number of operations of the form "reverse an arbitrary contiguous substring of the string" required to sort the string in non-descending order.
You have to find a binary string with the minimum possible cost among those that match the given pattern. If there are multiple answers, print any of them.
Let's say that the binary string (a string where each character is either 0 or 1) matches the pattern if you can replace each character ? with 0 or 1 (for each character, the choice is independent) so that the strings become equal. For example, 0010 matches ?01?, but 010 doesn't match 1??, ??, or ????.
Let's define the cost of the binary string as the minimum number of operations of the form "reverse an arbitrary contiguous substring of the string" required to sort the string in non-descending order.
You have to find a binary string with the minimum possible cost among those that match the given pattern. If there are multiple answers, print any of them.
输入格式
The first line contains a single integer $t$ ( $1 \le t \le 3 \cdot 10^4$ ) — the number of test cases.
The first and only line of each test case contains the string $s$ ( $1 \le |s| \le 3 \cdot 10^5$ ) consisting of characters 0, 1, and/or ?.
The sum of the string lengths over all test cases does not exceed $3 \cdot 10^5$ .
The first and only line of each test case contains the string $s$ ( $1 \le |s| \le 3 \cdot 10^5$ ) consisting of characters 0, 1, and/or ?.
The sum of the string lengths over all test cases does not exceed $3 \cdot 10^5$ .
输出格式
For each test case, print a binary string with the minimum possible cost among those that match the given pattern. If there are multiple answers, print any of them.
输入输出样例
输入 #1
4 ??01? 10100 1??10? 0?1?10?10
输出 #1
00011 10100 111101 011110010
In the first test case of the example, the cost of the resulting string is $0$ .
In the second test case, the cost of the resulting string is $2$ : we can reverse the substring from the $1$ -st character to the $5$ -th character, and we obtain the string 00101. Then we reverse the substring from the $3$ -rd to the $4$ -th character, and we obtain the string 00011, which is sorted in non-descending order.
In the second test case, the cost of the resulting string is $2$ : we can reverse the substring from the $1$ -st character to the $5$ -th character, and we obtain the string 00101. Then we reverse the substring from the $3$ -rd to the $4$ -th character, and we obtain the string 00011, which is sorted in non-descending order.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted