CF2223A.Zhily and Bracket Swapping

普及-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

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 aa and bb of even length nn. You can perform the following operation any number of times:

  • Choose a position ii (1≤i≤n1 \leq i \leq n) and swap aia_i and bib_i.

Determine whether you can turn both aa and bb into regular bracket sequences.

在荒野深处,芝莉和吉莉发现了两个由括号组成的序列。每个序列都蕴含某种逻辑结构,但它们各自本身未必是合法的括号序列。她们发现,通过在两个序列之间交换括号,便可以修复它们。她们希望通过对两个序列之间的括号进行交换,使得两个序列均变为合法的括号序列。

一个合法的括号序列是指仅由 '(' 和 ')' 构成的序列,且可通过在其中任意位置插入数字 1 和加号 +(可多次插入)而构成一个合法的数学表达式。例如,序列 "()(()())" 是一个合法的括号序列,而 "())((" 或 "((" 则不是合法的括号序列。

给定两个长度为偶数 nn 的括号序列 aa 和 bb。你可以执行以下操作任意多次:

  • 选择一个位置 ii(1≤i≤n1 \leq i \leq n),并交换 aia_i 与 bib_i。

请判断是否能通过若干次上述操作,使得 aa 和 bb 均变为合法的括号序列。

输入格式

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

The first line of each test case contains a single integer nn (2≤n≤2⋅1052 \leq n \leq 2\cdot 10^5, nn is even) — the length of the strings aa and bb.

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

The third line of each test case contains a sequence bb of length nn consisting only of characters '(' and ')'.

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \leq n \leq 2\cdot 10^5,且 nn 为偶数)—— 字符串 aa 和 bb 的长度。

每个测试用例的第二行包含一个长度为 nn 的序列 aa,其中仅包含字符 '(' 和 ')'。

每个测试用例的第三行包含一个长度为 nn 的序列 bb,其中仅包含字符 '(' 和 ')'。

保证所有测试用例的 nn 之和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, output "YES" if you can turn both aa and bb 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).

对于每个测试用例,如果可以将 aa 和 bb 都变为合法的括号序列,则输出 “YES”,否则输出 “NO”。你可以在任意大小写形式下输出 “YES” 和 “NO”(例如,“yES”、“yes” 和 “Yes” 均被视为肯定回答)。

输入输出样例

  • 输入#1

    7
    2
    ()
    ()
    4
    ))((
    (())
    4
    ((((
    ))))
    4
    ()()
    (())
    6
    (((())
    ()()))
    8
    ()()()()
    (((())))
    4
    ((((
    ((((

    输出#1

    YES
    NO
    NO
    YES
    YES
    YES
    NO

说明/提示

In the first, fourth, and sixth test cases, both aa and bb are initially regular bracket sequences.

In the second and third test cases, either a1a_1 or b1b_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.

在第一、第四和第六个测试用例中,aa 和 bb 初始均为合法括号序列。

在第二和第三个测试用例中,a1a_1 或 b1b_1 中必有一个为 ')',因此它们不可能是合法括号序列。

在第五个测试用例中,最终的序列可能为 ((()))\texttt{(((}\color{red}{\texttt{)}}\texttt{))} 和 ()(())\texttt{()(}\color{red}{\texttt{(}}\texttt{))},其中需交换的括号以红色标出。

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

首页