CF1458D.Flip and Reverse
NOI/NOI+/CTSC
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a string s of 0's and 1's. You are allowed to perform the following operation:
- choose a non-empty contiguous substring of s that contains an equal number of 0's and 1's;
- flip all characters in the substring, that is, replace all 0's with 1's, and vice versa;
- reverse the substring.
For example, consider s = 00111011, and the following operation:
- Choose the first six characters as the substring to act upon: 00111011. Note that the number of 0's and 1's are equal, so this is a legal choice. Choosing substrings 0, 110, or the entire string would not be possible.
- Flip all characters in the substring: 11000111.
- Reverse the substring: 10001111.
Find the lexicographically smallest string that can be obtained from s after zero or more operations.
给你一个由 0 和 1 组成的字符串 s。你可以执行以下操作任意次(包括零次):
- 选择 s 中一个非空的连续子串,该子串中
0的个数与1的个数相等; - 将该子串中所有字符翻转(即所有
0变为1,所有1变为0); - 将该子串反转。
例如,考虑 s=00111011,以及如下操作:
- 选取前六个字符作为操作子串:00111011。注意该子串中
0和1的个数相等,因此这是一个合法的选择。而子串 0、110 或整个字符串则不满足条件,因此不可选。 - 翻转子串中所有字符:11000111。
- 反转该子串:10001111。
求经过零次或多次上述操作后,能从 s 得到的字典序最小的字符串。
输入格式
The first line contains a single integer T (1≤T≤5⋅105) — the number of test cases. Each of the following T lines contains a single non-empty string — the input string s for the respective test case.
All strings consist of characters 0 and 1, and their total length does not exceed 5⋅105.
第一行包含一个整数 T(1≤T≤5⋅105)——测试用例的数量。接下来的 T 行中,每行包含一个非空字符串 —— 对应测试用例的输入字符串 s。
所有字符串均由字符 0 和 1 组成,且所有字符串的总长度不超过 5⋅105。
输出格式
For each test case, on a separate line print the lexicographically smallest string that can be obtained from s after zero or more operations.
对于每个测试用例,在单独一行中输出对字符串 s 执行零次或多次操作后所能得到的字典序最小的字符串。
输入输出样例
输入#1
3 100101 1100011 10101010
输出#1
010110 0110110 10101010
说明/提示
In the first test case a single operation should be applied to the entire string.
In the second test case two operations are needed: 0111001, 0110110.
In the third test case the string stays the same after any operation.
在第一个测试用例中,应对整个字符串执行一次操作。
在第二个测试用例中,需要执行两次操作:0111001、0110110。
在第三个测试用例中,字符串在执行任意操作后均保持不变。
输入解题思路,AI测评打分。不知道怎么写?