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.
An operation always touches one odd position and one even position. So the raw number of opening brackets is not the whole story; split opens by position parity.
Track . Flipping (( to )) or )) to (( changes both sides by the same amount, so never changes.
For a regular bracket sequence of length , the first character must be ( and the last must be ). If it has opening brackets on even positions, then .
So the only possible values are . Equivalently, must be an integer with .
That condition is also sufficient. Same-parity characters can be rearranged using two operations on length-3 blocks, and then counts can be adjusted in odd/even pairs. So just construct any RBS with the required even-position opens.
Core idea
Use a parity invariant. Positions are 1-indexed. Define
.
Every operation flips two adjacent equal brackets. Adjacent positions have opposite parity. If we flip (( into )), we remove one opening bracket from an odd position and one from an even position. If we flip )) into ((, we add one opening bracket to each side. Either way, the difference stays unchanged.
This is stronger than just saying the number of ( keeps the same parity. That weaker invariant misses dumb cases like )(: it has one opening bracket, but it is obviously stuck and not regular. The parity split is the part that actually matters.
Which values can a regular sequence have?
Let . If is odd, stop immediately: no regular bracket sequence has odd length.
Now look at any regular bracket sequence of length :
(, and it is odd;), and it is even;Let be the number of opening brackets on even positions. Then the number of opening brackets on odd positions is , so
.
Also, the last position is even and must be ), so not all even positions can be opening brackets. Therefore
.
So a target exists only if
is an integer and .
Why this is sufficient
The invariant is not just necessary; it fully characterizes reachability for what we need.
Here is the key local trick. On any length-3 block whose two ends differ, two operations can swap those ends while leaving the middle character alone:
(() -> ))) -> )((
()) -> ((( -> ))(
The other two cases are symmetric. This means we can swap neighboring positions inside the odd-position subsequence, and also inside the even-position subsequence. Adjacent swaps let us permute each parity however we want.
After that, suppose two strings have the same . If one has too many opening brackets on odd positions, it has the same excess on even positions too, because the difference is fixed. We can permute positions so an odd ( and an even ( become adjacent, then flip (( to )). Similarly, if we need more opens, put two adjacent ) together and flip them to ((.
So any string with invariant can reach any other string with the same . We do not need to print the operations, thankfully, because that would be extra nonsense.
Constructing the answer
Once is valid, build this sequence:
( + copies of () + ) + copies of ()
Example with :
(()())()
This is always regular:
( opens an outer layer;() pairs never break balance;) closes the outer layer;() pairs.It has exactly opening brackets on even positions, so its invariant is exactly . Since it has the same invariant as the input, it is reachable.
Algorithm
For each test case:
-1.-1.(, then copies of (), then ), then copies of ().Complexity: per test case, and extra memory besides the output string.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n;
string s;
cin >> n >> s;
if (n % 2 == 1) {
cout << -1 << '\n';
continue;
}
int m = n / 2;
int d = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '(') {
if (i % 2 == 0) d++;
else d--;
}
}
int diff = m - d;
if (diff % 2 != 0) {
cout << -1 << '\n';
continue;
}
int q = diff / 2;
if (q < 0 || q > m - 1) {
cout << -1 << '\n';
continue;
}
string ans;
ans.reserve(n);
ans.push_back('(');
for (int i = 0; i < q; i++) {
ans.push_back('(');
ans.push_back(')');
}
ans.push_back(')');
for (int i = 0; i < m - q - 1; i++) {
ans.push_back('(');
ans.push_back(')');
}
cout << ans << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n;
string s;
cin >> n >> s;
if (n % 2 == 1) {
cout << -1 << '\n';
continue;
}
int m = n / 2;
int d = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '(') {
if (i % 2 == 0) d++;
else d--;
}
}
int diff = m - d;
if (diff % 2 != 0) {
cout << -1 << '\n';
continue;
}
int q = diff / 2;
if (q < 0 || q > m - 1) {
cout << -1 << '\n';
continue;
}
string ans;
ans.reserve(n);
ans.push_back('(');
for (int i = 0; i < q; i++) {
ans.push_back('(');
ans.push_back(')');
}
ans.push_back(')');
for (int i = 0; i < m - q - 1; i++) {
ans.push_back('(');
ans.push_back(')');
}
cout << ans << '\n';
}
}