Codeforces Round 1089 (Div. 2)
D
A Simple RBS Problem
Codeforces
2100
原题
← C2E →
翻译中…

A bracket sequence is called regular if it is possible to obtain a correct arithmetic expression by inserting characters $+$ and $1$ into this sequence. For example, sequences (())(), () and (()(())) are regular, while )(, (() and (()))( are not. Let's call a regular bracket sequence RBS.

翻译中…

You can perform the following operation on any RBS string $b$ —

翻译中…
  • Choose any two disjoint, non empty substrings$^{\text{∗}}$ of $b$ which are RBS and swap the two substrings.
翻译中…

More formally, let $m$ be the length of $b$. You can perform the following operation on $b$:

翻译中…
  • Choose indices $1 \le l_1 \lt r_1 \lt l_2 \lt r_2 \le m$ such that the substrings $b[{l_1..r_1}]$ and $b[l_2..r_2]$ are both RBS and swap these substrings. The string $b$ becomes $b_1 \ldots b_{l_1-1} b_{l_2} b_{l_2+1}\ldots b_{r_2} b_{r_1+1}\ldots b_{l_2-1} b_{l_1} b_{l_1+1}\ldots b_{r_1}b_{r_2+1} \ldots b_m$.
翻译中…

You are given two strings $s$ and $t$ of length $n$ ($n$ is even) such that both $s$ and $t$ are RBS.

翻译中…

Determine whether you can transform $s$ to $t$ using the above operation any number of times.

翻译中…

$^{\text{∗}}$A string $a$ is a substring of a string $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

翻译中…

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 an integer $n$ ($4\leq n\leq 5\cdot 10^5$, $n$ is even).

翻译中…

The second line of each test case contains a string $s$ of length $n$ such that $s$ is an RBS.

翻译中…

The third line of each test case contains a string $t$ of length $n$ such that $t$ is an RBS.

翻译中…

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

翻译中…

Output

翻译中…

For each test case, print "YES" if $s$ can be transformed to $t$, and "NO" otherwise.

翻译中…

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

翻译中…

Note

翻译中…

In the first test case, the two strings are already same.

翻译中…

In the third test case, we can choose $l_1=1,r_1=4,l_2=5,r_2=6$ and transform $s$ to $t$.

翻译中…

Here is a visualization of the operation performed in this case —

翻译中…

$\color{red}{(())}\color{blue}{()}\rightarrow\color{blue}{()}\color{red}{(())}$

翻译中…

样例

Input #1
5
4
()()
()()
4
()()
(())
6
(())()
()(())
8
(()()())
()()(())
8
()()(())
()(()())
Output #1
YES
NO
YES
NO
YES
InputCopy OutputCopy
题目信息
题号
2210D
时间限制
2 s
内存限制
256 MB
标签
构造
数学
字符串
树

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