Balance the Bits CodeForces - 1504C( 字符串 + 思维 )

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.

题意:

给一个t表示t组样例

给一个n表示接下来字符串的长度

需要我们构造两个括号序列?如何构造?

字符串中的1表示需要我们构造的括号序列第一个和第二个符号是一样的,0表示两个符号不一样

如果构造的括号序列合法则输出YES和我们构造的两个序列
否则,输出NO

思路:

首先考虑输出NO的情况

当括号序列合法时,肯定序列第一个是 “( ”最后一个是 “)”,并且1的个数为偶数或者是0的个数为偶数

当判断题目所给的字符串可以构造出合法的括号序列,问如何构造?

当为1时,两个字符串对应位置上的符号相等,当为0时,不等,可以构造的话1和0的个数都是偶数,那么前一半数量的1输出“(”,后一半数量的1输出“)”,为0的时候交替输出,即0的数量为第奇数个输出“(”,第偶数个输出“)”

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[200010],b[200010],c[200010];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,ans0=0,ans1=0;
        scanf("%d",&n);
        scanf("%s",a);
        for(int i=0; i<n; i++)
        {
            if(a[i]=='1')
                ans1++;
            else if(a[i]=='0')
                ans0++;
        }
        if(a[0]!='1'||a[n-1]!='1'||ans1%2||ans0%2)
            printf("NO\n");
        else
        {
            int k=0,k1=0,s=ans1/2;
            for(int i=0; i<n; i++)
            {

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值