Balance the Bits(构造)

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 s of length n. Construct two balanced bracket sequences a and b of length n such that for all 1≤i≤n:

if si=1, then ai=bi
if si=0, then ai≠bi
If it is impossible, you should report about it.

Input

The first line contains a single integer t (1≤t≤104) — the number of test cases.

The first line of each test case contains a single integer n (2≤n≤2⋅105, n is even).

The next line contains a string s of length n, consisting of characters 0 and 1.

The sum of n across all test cases does not exceed 2⋅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 a and b satisfying the conditions on the next two lines.

If there are multiple solutions, you may print any.

Example Input

3
6
101101
10
1001101101
4
1100

Output

YES
()()()
((()))
YES
()()((()))
(())()()()
NO

Note

In the first test case, a="()()()" and b="((()))". The characters are equal in positions 1, 3, 4, and 6, which are the exact same positions where si=1.

In the second test case, a="()()((()))" and b="(())()()()". The characters are equal in positions 1, 4, 5, 7, 8, 10, which are the exact same positions where si=1.

In the third test case, there is no solution.

题意:给你一个0、1字符串,构造两个合理的额括号匹配,当为1是,这两个括号是相同方向的,当为0时,这两个括号方向是相反的,如果不存在输出NO,存在输出YES,并输出这两个合理的括号。

题解:字符串的第一位和最后一位有一个是0,就不可能构造出两个合理的括号匹配。对于前一半的1,构造成“( ”,后一半的1,构造成“ )”。对于奇数位的0,a数组构造成“ ( ”,b数组构造成“ )”,对于偶数位的0,a数组构造成“ )”,b数组构造成“( ”。

AC代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=21e5+10;
char s[maxn],a[maxn],b[maxn];
int a1id[maxn],a0id[maxn];

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		int ad=0,bd=0;
		scanf("%d",&n);
		scanf("%s",s);
		int d1=0,d0=0;
		int i,j,k;
		if(s[0]!='1'||s[n-1]!='1')
		{
			printf("NO\n");
			continue;
		}
		for(i=0; i<n; i++)
		{
			if(s[i]=='1')
				a1id[++d1]=i;
			else
				a0id[++d0]=i;
		}
		if(d1%2!=0)
		{
			printf("NO\n");
			continue;
		}
		for(i=1; i<=d1/2; i++)
		{
			a[a1id[i]]='(';
			b[a1id[i]]='(';
		}

		for(i=d1/2+1; i<=d1; i++)
		{
			a[a1id[i]]=')';
			b[a1id[i]]=')';
		}

		for(i=1; i<=d0; i++)
		{
			if(i%2!=0)
			{
				a[a0id[i]]='(';
				b[a0id[i]]=')';
			}
			else
			{
				a[a0id[i]]=')';
				b[a0id[i]]='(';
			}
		}
		printf("YES\n");
		for(i=0; i<n; i++)
			printf("%c",a[i]);
		printf("\n");
		for(i=0; i<n; i++)
			printf("%c",b[i]);
		printf("\n");
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值