Codeforces 1167D - Bicolored RBS

题目链接:http://codeforces.com/problemset/problem/1167/D


题意:题目定义RBS,给你一个字符串,你要对其所有字符染色,使之分解为俩个RBS,使俩个RBS深度最大值(内括号)最小化,用0和1输出染色方案。

思路:贪心,从左往右遍历,一边分一个,用 m 统计 ‘( ’ 的数量,m为 奇数分给左边,偶数分给右边,为了满足俩边左右括号数目能一致,当遇到 ‘ )’的时候应该先和最近的 ‘( ’配对 ,再调整 m。

AC代码 :

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 int main()
 7 {
 8     string a;
 9     int n;
10     cin >> n >> a;
11     int m = 0;
12     for(int i = 0;i < a.size();i++)
13     {
14         if(a[i] == '(')
15         {
16             m++;
17             cout << m % 2;
18         }
19         else
20         {
21             cout << m % 2 ;
22             m--;
23         }
24     }
25     return  0;
26 }

猜你喜欢

转载自www.cnblogs.com/Carered/p/10947216.html
rbs