Educational Codeforces Round 65 (Rated for Div. 2) D. Bicolored RBS

博客围绕链接https://codeforces.com/contest/1167/problem/D的题目展开。题目给出一个偶数长度的正则括号序列(RBS),要求将括号染成红、蓝两色,使红、蓝括号各自构成RBS,且最小化二者的最大嵌套深度。解题思路是用双指针,每次变换颜色。代码转载自指定博客。

链接:https://codeforces.com/contest/1167/problem/D

题意:

A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular (shortly, RBS) if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are RBS and ")(" and "(()" are not.

We can see that each opening bracket in RBS is paired with some closing bracket, and, using this fact, we can define nesting depth of the RBS as maximum number of bracket pairs, such that the 22-nd pair lies inside the 11-st one, the 33-rd one — inside the 22-nd one and so on. For example, nesting depth of "" is 00, "()()()" is 11 and "()((())())" is 33.

Now, you are given RBS ss of even length nn. You should color each bracket of ss into one of two colors: red or blue. Bracket sequence rr, consisting only of red brackets, should be RBS, and bracket sequence, consisting only of blue brackets bb, should be RBS. Any of them can be empty. You are not allowed to reorder characters in ss, rr or bb. No brackets can be left uncolored.

Among all possible variants you should choose one that minimizes maximum of rr's and bb's nesting depth. If there are multiple solutions you can print any of them.

思路:

双指针往后跑就行了,每次变一个颜色。

代码:

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 2e5+10;
int res[MAXN];

int main()
{
    int n;
    string s;
    cin >> n >> s;
    int color = 0;
    int l = 0, r = 0;
    while (s[r] != ')')
        r++;
    int cnt = 0;
    while (cnt < n)
    {
        res[l++] = color;
        res[r++] = color;
        cnt += 2;
        while (l < n && s[l] != '(')
            l++;
        while (r < n && s[r] != ')')
            r++;
        color ^= 1;
    }
    for (int i = 0;i < n;i++)
        cout << res[i];
    cout << endl;

    return 0;
}

  

转载于:https://www.cnblogs.com/YDDDD/p/10900276.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值