题解:符合要求的01串需满足首尾为1且0,1的个数都为偶数。括号序列的构造:前k/2个1为左括号,后k/2个1为右括号,0位置上左右括号轮换。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char a[200005],b[200005];
bool check(char *c)
{
stack<int> s;
int i=0;
while(c[i])
{
if(c[i]=='(')s.push(1);
else
{
if(s.empty())return 0;
s.pop();
}
i++;
}
if(s.size())return 0;
return 1;
}
void solve(){
int n;
cin>>n;
fill(a,a+n+1,0);
fill(b,b+n+1,0);
string t;
cin>>t;
int cnt=0;
for(int i=0;i<n;i++)
{
if(t[i]=='1')cnt++;
}
if( cnt%2==1 || n%2==1)
{
cout<<"NO"<<endl;
return ;
}
int i=0,j=n-1;
while(i<j)
{
while(t[i]=='0')i++;
while(t[j]=='0')j--;
if(i>j)break;
a[i]=b[i]='(';
b[j]=a[j]=')';
i++;
j--;
}
int op=0;
for(int k=0;k<n;k++)
{
if(a[k]==0)
{
if(op){
a[k]='(';
b[k]=')';
}
else {
a[k]=')';
b[k]='(';
}
op^=1;
}
}
if(check(a) && check(b))
{
cout<<"YES"<<endl;
cout<<a<<endl<<b<<endl;
}
else cout<<"NO"<<endl;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int T;
cin>>T;
while(T--)solve();
return 0;
}