C. Balance the Bits (Codeforces Round #712 (Div. 2))

题目链接:

https://codeforces.com/contest/1504/problem/C

题意:

待补全:

思路:

待补全:

代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 110;
typedef pair<int, int> PII;
typedef long long LL;

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        string s;
        cin >> s;
        string a, b;
        a.resize(n);
        b.resize(n);
        int cnt0 = 0, cnt1 = 0;
        for(int i = 0; i < n; i++)
        {
            if(s[i] == '1') cnt1++;
            else    cnt0++;
        }
        //如果1的个数和0的个数不为偶数,那么必然失败
        if(cnt0 % 2 || cnt1 % 2)    cout  << "NO" << endl;
        //如果第一位和最后一位不为都1,那么必然失败
        else if(s[0] == '0' || s[n - 1] == '0') cout  << "NO" << endl;
        //除此之外,必然成功
        else
        {
            a[0] = b[0] = '(';
            a[n - 1] = b[n - 1] = ')';
            int idx1 = 2;

            int le = 0;

            for(int i = 1; i < n - 1; i++)
            {
                if(s[i] == '1')
                {
                    if(idx1 <= cnt1 / 2)
                    {
                        a[i] = b[i] = '(';
                    }
                    else
                    {
                        a[i] = b[i] = ')';
                    }
                    idx1++;
//                    cout << a[i] << endl;
                }
                else
                {
                    if(le % 2 == 1)
                    {
                        a[i] = ')';
                        b[i] = '(';
                    }
                    else
                    {
                        a[i] = '(';
                        b[i] = ')';
                    }
                    le++;
                }
            }
            cout << "YES" << endl;
            cout << a << endl;
            cout << b << endl;
        }

    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值