CF1837C.Best Binary String

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a string ss 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.

给你一个由字符 0、1 和/或 ? 组成的字符串 ss。我们称其为一个模式。

若一个二进制字符串(即每个字符均为 0 或 1 的字符串)能通过将模式中每个 ? 独立地替换为 0 或 1 后与该二进制字符串完全相等,则称该二进制字符串匹配该模式。例如,0010 匹配 ?01?,但 010 不匹配 1??、?? 或 ????。

我们定义一个二进制字符串的代价为:将其排序成非降序(即所有 0 在前、所有 1 在后)所需的最少操作次数,其中每次操作为“翻转该字符串中任意一个连续子串”。

你需要在所有匹配给定模式的二进制字符串中,找出一个代价最小的字符串。如有多个满足条件的答案,输出任意一个即可。

输入格式

The first line contains a single integer tt (1≤t≤3⋅1041 \le t \le 3 \cdot 10^4) — the number of test cases.

The first and only line of each test case contains the string ss (1≤∣s∣≤3⋅1051 \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⋅1053 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤3⋅1041 \le t \le 3 \cdot 10^4)—— 表示测试用例的数量。

每个测试用例仅有一行,包含字符串 ss(1≤∣s∣≤3⋅1051 \le |s| \le 3 \cdot 10^5),该字符串由字符 0、1 和/或 ? 组成。

所有测试用例的字符串长度总和不超过 3⋅1053 \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 00.

In the second test case, the cost of the resulting string is 22: we can reverse the substring from the 11-st character to the 55-th character, and we obtain the string 00101. Then we reverse the substring from the 33-rd to the 44-th character, and we obtain the string 00011, which is sorted in non-descending order.

在示例的第一个测试用例中,所得字符串的代价为 00。

在第二个测试用例中,所得字符串的代价为 22:我们可以将第 11 个字符到第 55 个字符构成的子串进行翻转,得到字符串 00101;然后将第 33 个字符到第 44 个字符构成的子串进行翻转,得到字符串 00011,该字符串已按非降序排列。

输入解题思路,AI测评打分。不知道怎么写?

首页