Codeforces Round 1097 (Div. 1, Based on Zhili Cup 2026)
A
Zhily and Bracket Swapping
Codeforces
1300
原题
←B →
翻译中…

Deep in the wilderness, Zhily and Jily discovered two sequences composed of brackets. Each sequence carries some logical structure, but neither of them is necessarily a regular bracket sequence on its own. They found that by swapping brackets between the two sequences, they could repair them. They wish to make both sequences regular bracket sequences by swapping brackets between them.

翻译中…

A regular bracket sequence is a sequence consisting of '(' and ')', which can be turned into a valid math expression by inserting 1 and + any number of times into the sequence. For example, the sequence "()(()())" is a regular bracket sequence, while "())((" or "(()" are not regular bracket sequences.

翻译中…

You are given two bracket sequences $a$ and $b$ of even length $n$. You can perform the following operation any number of times:

翻译中…
  • Choose a position $i$ ($1 \leq i \leq n$) and swap $a_i$ and $b_i$.
翻译中…

Determine whether you can turn both $a$ and $b$ into regular bracket sequences.

翻译中…

Input

翻译中…

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

翻译中…

The first line of each test case contains a single integer $n$ ($2 \leq n \leq 2\cdot 10^5$, $n$ is even) — the length of the strings $a$ and $b$.

翻译中…

The second line of each test case contains a sequence $a$ of length $n$ consisting only of characters '(' and ')'.

翻译中…

The third line of each test case contains a sequence $b$ of length $n$ consisting only of characters '(' and ')'.

翻译中…

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 "YES" if you can turn both $a$ and $b$ into a regular bracket sequences, "NO" otherwise. You can output "YES" and "NO" in any case (for example, "yES", "yes", and "Yes" will be recognized as a positive response).

翻译中…

Note

翻译中…

In the first, fourth, and sixth test cases, both $a$ and $b$ are initially regular bracket sequences.

翻译中…

In the second and third test cases, either $a_1$ or $b_1$ will be ')', which makes it impossible for them to be regular.

翻译中…

In the fifth test case, the final sequences may be $\texttt{(((}\color{red}{\texttt{)}}\texttt{))}$ and $\texttt{()(}\color{red}{\texttt{(}}\texttt{))}$, where the brackets to be swapped are colored red.

翻译中…

样例

Input #1
7
2
()
()
4
))((
(())
4
((((
))))
4
()()
(())
6
(((())
()()))
8
()()()()
(((())))
4
((((
((((
Output #1
YES
NO
NO
YES
YES
YES
NO
InputCopy OutputCopy
题目信息
题号
2223A
时间限制
2 s
内存限制
512 MB
标签
构造
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。