310837: CF1896F. Bracket Xoring

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

F. Bracket Xoringtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

You are given a binary string $s$ of length $2n$ where each element is $\mathtt{0}$ or $\mathtt{1}$. You can do the following operation:

  1. Choose a balanced bracket sequence$^\dagger$ $b$ of length $2n$.
  2. For every index $i$ from $1$ to $2n$ in order, where $b_i$ is an open bracket, let $p_i$ denote the minimum index such that $b[i,p_i]$ is a balanced bracket sequence. Then, we perform a range toggle operation$^\ddagger$ from $i$ to $p_i$ 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 $\mathtt{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 $\mathtt{0}$, there exists a way that requires no more than $10$ operations.

$^\dagger$ 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.

$^\ddagger$ If we perform a range toggle operation from $l$ to $r$ on a binary string $s$, then we toggle all values of $s_i$ such that $l \leq i \leq r$. If $s_i$ is toggled, we will set $s_i := \mathtt{0}$ if $s_i = \mathtt{1}$ or vice versa. For example, if $s=\mathtt{1000101}$ and we perform a range toggle operation from $3$ to $5$, $s$ will be changed to $s=\mathtt{1011001}$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 2\cdot 10^5$) — where $2n$ is the length of string $s$.

The second line of each test case contains a binary string $s$ of length $2n$ ($s_i = \mathtt{0}$ or $s_i = \mathtt{1}$).

It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.

Output

For each test case, output $-1$ in a single line if it is impossible to change all elements of $s$ to $\mathtt{0}$.

Otherwise, output a single integer $k$ ($0 \le k \le 10$) representing the number of operations needed to change all elements of $s$ to $\mathtt{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 $\mathtt{0}$ that require not more than $10$ operations, you can output any of them.

ExampleInput
4
1
01
2
0000
3
100111
4
01011100
Output
-1
2
()()
()()
1
(())()
2
(((())))
()()(())
Note

In the first test case, it can be proven that it is impossible to change all elements of $s$ to $\mathtt{0}$.

In the second test case, the first operation using the bracket sequence $b = \mathtt{()()}$ will convert the binary string $s=\mathtt{0000}$ to $s=\mathtt{1111}$. Then, the second operation using the same bracket sequence $b = \mathtt{()()}$ will convert the binary string $s=\mathtt{1111}$ back to $s=\mathtt{0000}$. Note that since all elements of $s$ is already $\mathtt{0}$ initially, using $0$ operations is also a valid answer.

In the third test case, a single operation using the bracket sequence $b = \mathtt{(())()}$ will change all elements of $s$ to $\mathtt{0}$. The operation will happen as follows.

  1. $b_1$ is an open bracket and $p_1 = 4$ since $b[1,4]=\mathtt{(())}$ is a balanced bracket sequence. Hence, we do a range toggle operation from $1$ to $4$ on the binary string $s = \mathtt{100111}$ to obtain $s = \mathtt{011011}$.
  2. $b_2$ is an open bracket and $p_2 = 3$ since $b[2,3]=\mathtt{()}$ is a balanced bracket sequence. Hence, we do a range toggle operation from $2$ to $3$ on the binary string $s = \mathtt{011011}$ to obtain $s = \mathtt{000011}$.
  3. $b_3$ is not an open bracket, so no range toggle operation is done at this step.
  4. $b_4$ is not an open bracket, so no range toggle operation is done at this step.
  5. $b_5$ is an open bracket and $p_5 = 6$ since $b[5,6]=\mathtt{()}$ is a balanced bracket sequence. Hence, we do a range toggle operation from $5$ to $6$ on the binary string $s = \mathtt{000011}$ to obtain $s = \mathtt{000000}$.
  6. $b_6$ 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 = \mathtt{(((())))}$ will convert the binary string $s = \mathtt{01011100}$ to $s = \mathtt{11111001}$. Then, the second operation using the bracket sequence $b = \mathtt{()()(())}$ will convert the binary string $s = \mathtt{11111001}$ to $s=\mathtt{00000000}$.

Output

题目大意:给定一个长度为 $2n$ 的二进制字符串 $s$,每个元素为 $\mathtt{0}$ 或 $\mathtt{1}$。你可以进行以下操作:

1. 选择一个长度为 $2n$ 的平衡括号序列 $b$。
2. 对于每个索引 $i$ 从 $1$ 到 $2n$,如果 $b_i$ 是一个开括号,令 $p_i$ 表示最小的索引,使得 $b[i,p_i]$ 是一个平衡括号序列。然后,我们在 $s$ 上从 $i$ 到 $p_i$ 执行一个范围切换操作。注意,由于长度为 $2n$ 的平衡括号序列将有 $n$ 个开括号,因此我们将在 $s$ 上执行 $n$ 个范围切换操作。

任务是找到不超过 $10$ 个操作将 $s$ 的所有元素更改为 $\mathtt{0}$ 的序列,或者确定这是不可能的。注意,你不需要最小化操作的数量。

输入输出数据格式:

输入:
- 第一行包含一个整数 $t$($1 \le t \le 1000$),表示测试用例的数量。
- 每个测试用例包含以下内容:
- 第一行包含一个整数 $n$($1 \le n \le 2 \cdot 10^5$),$2n$ 是字符串 $s$ 的长度。
- 第二行包含一个长度为 $2n$ 的二进制字符串 $s$($s_i = \mathtt{0}$ 或 $s_i = \mathtt{1}$)。

输出:
- 对于每个测试用例,如果无法将 $s$ 的所有元素更改为 $\mathtt{0}$,则输出一行 $-1$。
- 否则,输出一个整数 $k$($0 \le k \le 10$),表示将 $s$ 的所有元素更改为 $\mathtt{0}$ 所需的操作数。然后在接下来的 $k$ 行中,输出一个长度为 $2n$ 的平衡括号序列,表示将 $s$ 的所有元素更改为 $\mathtt{0}$ 所需的操作。如果有多种方式在不超过 $10$ 个操作内将 $s$ 的所有元素更改为 $\mathtt{0}$,则可以输出其中任何一种。题目大意:给定一个长度为 $2n$ 的二进制字符串 $s$,每个元素为 $\mathtt{0}$ 或 $\mathtt{1}$。你可以进行以下操作: 1. 选择一个长度为 $2n$ 的平衡括号序列 $b$。 2. 对于每个索引 $i$ 从 $1$ 到 $2n$,如果 $b_i$ 是一个开括号,令 $p_i$ 表示最小的索引,使得 $b[i,p_i]$ 是一个平衡括号序列。然后,我们在 $s$ 上从 $i$ 到 $p_i$ 执行一个范围切换操作。注意,由于长度为 $2n$ 的平衡括号序列将有 $n$ 个开括号,因此我们将在 $s$ 上执行 $n$ 个范围切换操作。 任务是找到不超过 $10$ 个操作将 $s$ 的所有元素更改为 $\mathtt{0}$ 的序列,或者确定这是不可能的。注意,你不需要最小化操作的数量。 输入输出数据格式: 输入: - 第一行包含一个整数 $t$($1 \le t \le 1000$),表示测试用例的数量。 - 每个测试用例包含以下内容: - 第一行包含一个整数 $n$($1 \le n \le 2 \cdot 10^5$),$2n$ 是字符串 $s$ 的长度。 - 第二行包含一个长度为 $2n$ 的二进制字符串 $s$($s_i = \mathtt{0}$ 或 $s_i = \mathtt{1}$)。 输出: - 对于每个测试用例,如果无法将 $s$ 的所有元素更改为 $\mathtt{0}$,则输出一行 $-1$。 - 否则,输出一个整数 $k$($0 \le k \le 10$),表示将 $s$ 的所有元素更改为 $\mathtt{0}$ 所需的操作数。然后在接下来的 $k$ 行中,输出一个长度为 $2n$ 的平衡括号序列,表示将 $s$ 的所有元素更改为 $\mathtt{0}$ 所需的操作。如果有多种方式在不超过 $10$ 个操作内将 $s$ 的所有元素更改为 $\mathtt{0}$,则可以输出其中任何一种。

加入题单

上一题 下一题 算法标签: