Codeforces Round #106 (Div. 2) D. Coloring Brackets —— 区间DP
题目链接:https://vjudge.net/problem/CodeForces-149D
2 seconds
256 megabytes
standard input
standard output
Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solution. Today you will face it.
You are given string s. It represents a correct bracket sequence. A correct bracket sequence is the sequence of opening ("(") and closing (")") brackets, such that it is possible to obtain a correct mathematical expression from it, inserting numbers and operators between the brackets. For example, such sequences as "(())()" and "()" are correct bracket sequences and such sequences as ")()" and "(()" are not.
In a correct bracket sequence each bracket corresponds to the matching bracket (an opening bracket corresponds to the matching closing bracket and vice versa). For example, in a bracket sequence shown of the figure below, the third bracket corresponds to the matching sixth one and the fifth bracket corresponds to the fourth one.

You are allowed to color some brackets in the bracket sequence so as all three conditions are fulfilled:
- Each bracket is either not colored any color, or is colored red, or is colored blue.
 - For any pair of matching brackets exactly one of them is colored. In other words, for any bracket the following is true: either it or the matching bracket that corresponds to it is colored.
 - No two neighboring colored brackets have the same color.
 
Find the number of different ways to color the bracket sequence. The ways should meet the above-given conditions. Two ways of coloring are considered different if they differ in the color of at least one bracket. As the result can be quite large, print it modulo 1000000007 (109 + 7).
The first line contains the single string s (2 ≤ |s| ≤ 700) which represents a correct bracket sequence.
Print the only number — the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007(109 + 7).
(())
12
(()())
40
()
4
Let's consider the first sample test. The bracket sequence from the sample can be colored, for example, as is shown on two figures below.


The two ways of coloring shown below are incorrect.


题解:
给出一串合法的括号,为括号上色,有如下原则:1)一对括号有且仅有一个是涂上颜色的, 2)颜色只有两种, 3)相邻的括号的颜色不允许相同(除非都没上色)。问:满足上述三个条件的上色方案有多少种?
1.由于给出的括号序列是合法的,即左括号与右括号一一对应。所以我们可以先预处理出每个左括号所对应的右括号。
2.详情请看代码注释。
写法一(人工枚举):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; LL dp[MAXN][MAXN][][];
int top, Stack[MAXN], match[MAXN];
char s[MAXN]; //区间为[l, r], isL为l-1处是否上色, isR为r+1处是否上色。
LL dfs(int l, int r, bool isL, bool isR)
{
if(l>=r) return ; //因为是乘法,所以遇到非法位置,就返回1。要是加法就return0或者1(实际情况实际考虑)。
if(dp[l][r][isL][isR]!=-) return dp[l][r][isL][isR]; LL ret = ;
int k = match[l]; //找到与最左端的左括号匹配的右括号 if(!isL) //首先考虑为左括号上色。如果l-1处没有上色, 那么左括号就可以上两种颜色
ret = (ret + (2LL*dfs(l+, k-, true, false)*dfs(k+, r, false, isR))%MOD)%MOD;
else //否则, 左括号只能上一种颜色,与l-1处括号相对的颜色
ret = (ret + (1LL*dfs(l+, k-, true, false)*dfs(k+, r, false, isR))%MOD)%MOD;
if(k!=r || (k==r&&!isR) ) //其次为右括号上色。如果右括号不在右端点,或者在右端点但是r+1处没有上色,则可上两种颜色
ret = (ret + (2LL*dfs(l+, k-, false, true)*dfs(k+, r, true, isR))%MOD)%MOD;
else //否则,右括号在最右端且r+1处上了颜色,那么右括号只能上一种颜色。
ret = (ret + (1LL*dfs(l+, k-, false, true)*dfs(k+, r, true, isR))%MOD)%MOD; return dp[l][r][isL][isR] = ret;
} int main()
{
while(scanf("%s", s+)!=EOF)
{
int n = strlen(s+);
top = ;
for(int i = ; i<=n; i++) //为左括号找到匹配的右括号
{
if(s[i]=='(') Stack[top++] = i;
else match[Stack[--top]] = i;
} memset(dp, -, sizeof(dp));
dfs(, n, , );
printf("%lld\n", dp[][n][][]);
}
}
写法二(for枚举,推荐):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; LL dp[MAXN][MAXN][][];
int top, Stack[MAXN], match[MAXN];
char s[MAXN]; //区间为[l, r],Lcol为l-1处的颜色, Rcol为r+1处的颜色,0代表没上色,1和2分别代表两种不同的颜色
LL dfs(int l, int r, int Lcol, int Rcol)
{
if(l>=r) return ; //因为是乘法,所以遇到非法位置,就返回1。要是加法就return0或者1(实际情况实际考虑)。
if(dp[l][r][Lcol][Rcol]!=-) return dp[l][r][Lcol][Rcol]; LL ret = ;
int k = match[l]; //找到与最左端的左括号匹配的右括号
for(int lc = ; lc<; lc++) //枚举这个括号的着色情况,并且需要去除掉非法的情况
for(int rc = ; rc<; rc++)
{
if((lc&&rc)||(!lc&&!rc)) continue; //如果两个括号都没涂色或者都涂上颜色,非法
if(lc && lc==Lcol) continue; //如果l-1处涂上了颜色,且l处也要涂上相同的颜色, 非法
if(k==r && rc && rc==Rcol) continue; //如果匹配的右括号在最右端,且r+1处涂上了颜色,又尝试为
//右括号涂上相同的颜色,非法。如果右括号不在最右端,那么就无需
//考虑r+1处的着色情况了,因为右括号右边的括号必定没有上色。
ret = (ret+(1LL*dfs(l+, k-, lc, rc)*dfs(k+, r, rc, Rcol))%MOD)%MOD; //统计合法的情况
}
return dp[l][r][Lcol][Rcol] = ret;
} int main()
{
while(scanf("%s", s+)!=EOF)
{
int n = strlen(s+);
top = ;
for(int i = ; i<=n; i++) //为左括号找到匹配的右括号
{
if(s[i]=='(') Stack[top++] = i;
else match[Stack[--top]] = i;
} memset(dp, -, sizeof(dp));
printf("%lld\n", dfs(, n, , ));
}
}
Codeforces Round #106 (Div. 2) D. Coloring Brackets —— 区间DP的更多相关文章
- Codeforces Round #106 (Div. 2) D. Coloring Brackets 区间dp
		
题目链接: http://codeforces.com/problemset/problem/149/D D. Coloring Brackets time limit per test2 secon ...
 - Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)
		
Coloring Trees Problem Description: ZS the Coder and Chris the Baboon has arrived at Udayland! They ...
 - Codeforces Round #369 (Div. 2) C. Coloring Trees (DP)
		
C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
 - Codeforces Round #369 (Div. 2)     C. Coloring Trees(简单dp)
		
题目:https://codeforces.com/problemset/problem/711/C 题意:给你n,m,k,代表n个数的序列,有m种颜色可以涂,0代表未涂颜色,其他代表已经涂好了,连着 ...
 - Codeforces Round #336 (Div. 2) D. Zuma(区间DP)
		
题目链接:https://codeforces.com/contest/608/problem/D 题意:给出n个宝石的颜色ci,现在有一个操作,就是子串的颜色是回文串的区间可以通过一次操作消去,问最 ...
 - Codeforces Round #367 (Div. 2) C. Hard problem(DP)
		
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
 - codeforces 149D Coloring Brackets (区间DP + dfs)
		
题目链接: codeforces 149D Coloring Brackets 题目描述: 给一个合法的括号串,然后问这串括号有多少种涂色方案,当然啦!涂色是有限制的. 1,每个括号只有三种选择:涂红 ...
 - CF149D. Coloring Brackets[区间DP !]
		
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...
 - codeforce 149D Coloring Brackets 区间DP
		
题目链接:http://codeforces.com/problemset/problem/149/D 继续区间DP啊.... 思路: 定义dp[l][r][c1][c2]表示对于区间(l,r)来说, ...
 
随机推荐
- centos7 搭建hadoop
			
参考文档:http://blog.csdn.net/xiaoxiangzi222/article/details/52757168 https://waylau.com/centos-7-instal ...
 - Java 并发编程中使用 ReentrantLock 替代 synchronized 关键字原语
			
Java 5 引入的 Concurrent 并发库软件包中,提供了 ReentrantLock 可重入同步锁,用来替代 synchronized 关键字原语,并可提供更好的性能,以及更强大的功能.使用 ...
 - Kafka windows下的安装
			
1. 安装JDK 1.1 安装文件:http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载JDK1.2 安装完成后需 ...
 - Python资料大全
			
说明:以下文章为转载,有英文原文和中文整理翻译,对原作者和译者的工作表示极大感谢!!! 英文原文:https://github.com/vinta/awesome-python 中文译文:https: ...
 - 微软自带的异步Ajax请求
			
一.使用步骤 二.示例代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...
 - php使用strpos,strstr,strchr注意啦,若是数字查找则会当成ASCII码处理
			
strpos,strstr,strchr都是查找某字符出现的位置,若未找到,则返回false(判断是===) 如: var_dump(strpos("oa",'97')); var ...
 - Qt5官方demo解析集30——Extending QML - Binding Example
			
本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集29--Extendin ...
 - ZT:与其怨天尤人,不如全力以赴;若想改变世界,你必须先从改变自己开始!
			
在闻名世界的威斯特敏斯特大教堂地下室的墓碑林中,有一块名扬世界的墓碑.其实这只是一块很普通的墓碑,粗糙的花岗石质地,造型也很一般,同周围那些质地上乘.做工优良的亨利三世到乔治二世等二十多位英国前国王墓 ...
 - Solidworks打印工程图超出范围了怎么办
			
打印预览,边框部分无法显示 页面设置,比例改为90%,试一下 正常了,如果你还是无法正常显示,就再改小比例
 - ios9定位服务的app进入后台三分钟收不到经纬度,应用被挂起问题及解决方式
			
原来定位服务是10分钟收不到定位信息就挂起定位,如今变为最短3分钟,预计都是为了省电吧. 仅仅要你开启应用的后台定位,而且10分钟有一次定位,那么苹果就不会关闭你的线程.如今变成3分钟.若你的应用开启 ...