栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

题目链接:

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

题意:

待补全:

思路:

待补全:

代码:
#include
using namespace std;
const int N = 110;
typedef pair 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;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/347840.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号