CF1896F.Bracket Xoring
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a binary string s of length 2n where each element is 0 or 1 . You can do the following operation:
- Choose a balanced bracket sequence † b of length 2n .
- For every index i from 1 to 2n in order, where bi is an open bracket, let pi denote the minimum index such that b[i,pi] is a balanced bracket sequence. Then, we perform a range toggle operation ‡ from i to pi on s . Note that since a balanced bracket sequence of length 2n will have n open brackets, we will do n range toggle operations on s .
Your task is to find a sequence of no more than 10 operations that changes all elements of s to 0 , or determine that it is impossible to do so. Note that you do not have to minimize the number of operations.
Under the given constraints, it can be proven that if it is possible to change all elements of s to 0 , there exists a way that requires no more than 10 operations.
† A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters + and 1 . For example, sequences "(())()", "()", and "(()(()))" are balanced, while ")(", "(()", and "(()))(" are not.
‡ If we perform a range toggle operation from l to r on a binary string s , then we toggle all values of si such that l≤i≤r . If si is toggled, we will set si:=0 if si=1 or vice versa. For example, if s=1000101 and we perform a range toggle operation from 3 to 5 , s will be changed to s=1011001 .
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t ( 1≤t≤1000 ). The description of the test cases follows.
The first line of each test case contains a single integer n ( 1≤n≤2⋅105 ) — where 2n is the length of string s .
The second line of each test case contains a binary string s of length 2n ( si=0 or si=1 ).
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .
输出格式
For each test case, output −1 in a single line if it is impossible to change all elements of s to 0 .
Otherwise, output a single integer k ( 0≤k≤10 ) representing the number of operations needed to change all elements of s to 0 . Then, on each of the next k lines, output a balanced bracket sequence of length 2n representing the operations needed to change all elements of s to 0 s.
If there are multiple ways to change all elements of s to 0 that require not more than 10 operations, you can output any of them.
输入输出样例
输入#1
4 1 01 2 0000 3 100111 4 01011100
输出#1
-1 2 ()() ()() 1 (())() 2 (((()))) ()()(())
说明/提示
In the first test case, it can be proven that it is impossible to change all elements of s to 0 .
In the second test case, the first operation using the bracket sequence b=()() will convert the binary string s=0000 to s=1111 . Then, the second operation using the same bracket sequence b=()() will convert the binary string s=1111 back to s=0000 . Note that since all elements of s is already 0 initially, using 0 operations is also a valid answer.
In the third test case, a single operation using the bracket sequence b=(())() will change all elements of s to 0 . The operation will happen as follows.
- b1 is an open bracket and p1=4 since b[1,4]=(()) is a balanced bracket sequence. Hence, we do a range toggle operation from 1 to 4 on the binary string s=100111 to obtain s=011011 .
- b2 is an open bracket and p2=3 since b[2,3]=() is a balanced bracket sequence. Hence, we do a range toggle operation from 2 to 3 on the binary string s=011011 to obtain s=000011 .
- b3 is not an open bracket, so no range toggle operation is done at this step.
- b4 is not an open bracket, so no range toggle operation is done at this step.
- b5 is an open bracket and p5=6 since b[5,6]=() is a balanced bracket sequence. Hence, we do a range toggle operation from 5 to 6 on the binary string s=000011 to obtain s=000000 .
- b6 is not an open bracket, so no range toggle operation is done at this step.
In the fourth test case, the first operation using the bracket sequence b=(((()))) will convert the binary string s=01011100 to s=11111001 . Then, the second operation using the bracket sequence b=()()(()) will convert the binary string s=11111001 to s=00000000 .