Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Forget the raw parentheses for a moment. An RBS is an ordered rooted tree: each bracket pair is a node, and containment gives parent-child edges. What kind of tree object does an RBS substring correspond to?
An RBS substring is exactly a consecutive block of children of some node. So the operation is just swapping two contiguous child-blocks in this tree. The brackets are wearing a fake mustache; this is a tree problem.
Whole subtrees move, but no subtree is cracked open. A leaf stays a leaf, and an internal node stays internal. So the number of leaves is invariant:
Wrap the whole string in one extra pair. Now look from the new root downward while every node has exactly one child. This initial forced chain cannot be touched by any valid swap, because there is no disjoint block outside the only child subtree.
The answer is YES iff two values match: the leaf count and the forced-prefix depth . Sufficiency: at depth , make the first branching node have exactly children, then move all extra chain length into one child and leave the other children as leaves. Since swaps are reversible, both strings reaching the same canonical tree means they reach each other.
Take a string and wrap it as ( + + ). Now every bracket pair is a node, and the nearest enclosing bracket pair is its parent. The added pair is the root.
The key translation is:
So the operation becomes: choose two disjoint contiguous child-blocks, possibly under different parents, and swap those blocks. That's the whole damn problem after the disguise comes off.
A swap moves entire subtrees. It never opens a subtree and rearranges its insides. Also, the two parent nodes involved exchange non-empty child-blocks, so an internal node never becomes a leaf.
Therefore the number of leaves is invariant. In the string, a leaf is exactly an adjacent pair $\texttt{()}$, so
Define like this: starting from the whole string, while the first bracket matches the last bracket, strip that outer pair and add .
In the wrapped tree, is the length of the initial chain below the root where every node has exactly one child. Equivalently, it is the depth of the first node whose number of children is not .
Why is invariant? Every node on that initial chain has only one child, and that child contains everything below it. There is no disjoint RBS block outside that child subtree, so swaps cannot affect this chain. If the first non-unary node is branching, it also cannot become unary: to reduce it to one child, we would need to swap all its children with some disjoint single block outside them, but outside them there is nothing useful. If the whole tree is just a chain, no operation exists at all.
So both and are necessary.
Now suppose two strings have the same and . If , the tree is just one chain, so it is already uniquely determined. Boring, but correct.
Otherwise, let be the first branching node, at depth . The chain above is fixed, so we only care about the subtree below .
First, make have exactly children. Suppose . Then some descendant must be branching; otherwise each child of would be a chain and there would be exactly leaves. Choose a child of that is not an ancestor of ; this exists because is branching. Swap the one-subtree block with the whole child-list of .
Then
which strictly increases. It also cannot exceed , because every child of must contain at least one leaf. Repeat until .
At that point, each child of contains exactly one leaf, so every child subtree is just a chain. Pick one child of as the collector. For every other child that is not already a leaf, swap that whole child subtree with the bottom leaf of the collector chain. This makes the other child position a leaf and moves its extra length into the collector chain.
The final canonical tree is unique:
If the wrapped tree has
nodes, that collector chain has length
Thus every tree with the same pair can reach the same canonical tree. Since every operation is reversible, if and have the same , then
So the answer is exactly whether these two invariants match.
Compute by counting adjacent $\texttt{()}$.
Compute by matching brackets with a stack, then repeatedly checking whether the current leftmost opening bracket matches the current right endpoint.
The total complexity is
time and memory per test case.
Reference checked while preparing this explanation: official Codeforces editorial.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
pair<int, int> invariant(const string& s) {
int n = (int)s.size();
int leaves = 0;
for (int i = 0; i + 1 < n; ++i) {
if (s[i] == '(' && s[i + 1] == ')') ++leaves;
}
vector<int> match(n, -1), st;
st.reserve(n);
for (int i = 0; i < n; ++i) {
if (s[i] == '(') {
st.push_back(i);
} else {
int j = st.back();
st.pop_back();
match[j] = i;
}
}
int outer = 0;
int l = 0, r = n - 1;
while (l < r && match[l] == r) {
++outer;
++l;
--r;
}
return {leaves, outer};
}
int main() {
setIO();
int tc;
cin >> tc;
while (tc--) {
int n;
string s, t;
cin >> n >> s >> t;
cout << (invariant(s) == invariant(t) ? "YES\n" : "NO\n");
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
pair<int, int> invariant(const string& s) {
int n = (int)s.size();
int leaves = 0;
for (int i = 0; i + 1 < n; ++i) {
if (s[i] == '(' && s[i + 1] == ')') ++leaves;
}
vector<int> match(n, -1), st;
st.reserve(n);
for (int i = 0; i < n; ++i) {
if (s[i] == '(') {
st.push_back(i);
} else {
int j = st.back();
st.pop_back();
match[j] = i;
}
}
int outer = 0;
int l = 0, r = n - 1;
while (l < r && match[l] == r) {
++outer;
++l;
--r;
}
return {leaves, outer};
}
int main() {
setIO();
int tc;
cin >> tc;
while (tc--) {
int n;
string s, t;
cin >> n >> s >> t;
cout << (invariant(s) == invariant(t) ? "YES\n" : "NO\n");
}
}