A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not.
You are given a binary string ss of length nn. Construct two balanced bracket sequences aa and bb of length nn such that for all 1≤i≤n1≤i≤n:
If it is impossible, you should report about it.
Input
The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The first line of each test case contains a single integer nn (2≤n≤2⋅1052≤n≤2⋅105, nn is even).
The next line contains a string ss of length nn, consisting of characters 0 and 1.
The sum of nn across all test cases does not exceed 2⋅1052⋅105.
Output
If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower).
If the answer is "YES", output the balanced bracket sequences aa and bb satisfying the conditions on the next two lines.
If there are multiple solutions, you may print any.
Input:
3
6
101101
10
1001101101
4
1100
Out:
YES
()()()
((()))
YES
()()((()))
(())()()()
NO
题意:给一个偶数的二进制数字,让我们构造两个长度为n的平衡括号(如“()(())”就是平衡括号)。若s[i] == ‘0’,两个字符串对应位置的括号要相反。== ‘1’则相同。
题解:我们需要偶数个0来构造平衡括号,0时偶数的话,保证n时偶数,那么1的数量一定也会是偶数不然则不能构造。其次就是第一个和最后一个一定分别是“(”,")" ,因此s[0] == '1',s[n-1] -== '1'。不然也不能构造。
其余就是遇到1先添加“(”左括号,再次构造时就是“)”右括号,以此轮回。
遇到0,给两个待构造平衡括号分别添加两个相反的括号即可。
代码:
#include
#define int long long
using namespace std;
const int N = 2e5 + 10;
void solve(){
int n; cin >> n;
string s; cin >> s;
int c0 = 0;
for(int i = 0;i < n;i++) if(s[i] == '0') c0++;
if(s[0] != '1' || s[n-1] != '1' || (c0 & 1)){
cout <<"NO\n";
return;
}
string ans1="(",ans2 = "(";
int cnt1 = 0,cnt2 = 0;
for(int i = 1;i > t;
while(t--) solve();
return 0;
}