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
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值