CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数
区间DP
用栈先处理匹配
f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数
l和r匹配的话,转移到(l+1,r-1)
不匹配,i的匹配p一定在l和r之间,从p分开转移
听说用记忆化搜索比较快,可以像树形DP那样写记忆化搜索,也可以传统的四个参数那样写
用循环+条件判断,简化状态转移的枚举
注意细节 见代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=,MOD=1e9+;
char s[N];
long long n,f[N][N][][];
int st[N],top=,m[N];
void match(){
for(int i=;i<=n;i++){
if(s[i]=='(') st[++top]=i;
else{
int tmp=st[top--];
m[i]=tmp;
m[tmp]=i;
}
}
}
void dp(int l,int r){//printf("dp %d %d\n",l,r);
if(l>=r) return;
if(l+==r){
f[l][r][][]=f[l][r][][]=f[l][r][][]=f[l][r][][]=;
return;
}
if(m[l]==r){
dp(l+,r-);
for(int i=;i<;i++)
for(int j=;j<;j++){
if(j!=) f[l][r][][]=(f[l][r][][]+f[l+][r-][i][j])%MOD;
if(j!=) f[l][r][][]=(f[l][r][][]+f[l+][r-][i][j])%MOD;
if(i!=) f[l][r][][]=(f[l][r][][]+f[l+][r-][i][j])%MOD;
if(i!=) f[l][r][][]=(f[l][r][][]+f[l+][r-][i][j])%MOD;
}
}else{
int p=m[l];
dp(l,p);dp(p+,r);
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
for(int t=;t<;t++){
if(k==&&t==) continue;
if(k==&&t==) continue;
//if(i!=0&&t!=0) continue; 不需要,因为已保证这样的话值是0
f[l][r][i][j]=(f[l][r][i][j]+f[l][p][i][k]*f[p+][r][t][j]%MOD)%MOD;
}
}
//printf("%d %d %d %d %d %d\n",l,r,f[l][r][0][1],f[l][r][0][2],f[l][r][1][0],f[l][r][2][0]);
}
//void dp(int l,int r,int a,int b){
// int &ans=f[l][r][a][b];
// if(ans!=-1) return ans;
//
//}
int main(){
scanf("%s",s+);
n=strlen(s+);
match();
dp(,n);
long long ans=;
for(int i=;i<;i++)
for(int j=;j<;j++)
ans=(ans+f[][n][i][j])%MOD; printf("%d",ans);
}
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 modulo1000000007 (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.


CF149D. Coloring Brackets[区间DP !]的更多相关文章
- Codeforces Round #106 (Div. 2) D. Coloring Brackets —— 区间DP
题目链接:https://vjudge.net/problem/CodeForces-149D D. Coloring Brackets time limit per test 2 seconds m ...
- codeforces 149D Coloring Brackets (区间DP + dfs)
题目链接: codeforces 149D Coloring Brackets 题目描述: 给一个合法的括号串,然后问这串括号有多少种涂色方案,当然啦!涂色是有限制的. 1,每个括号只有三种选择:涂红 ...
- Codeforces Round #106 (Div. 2) D. Coloring Brackets 区间dp
题目链接: http://codeforces.com/problemset/problem/149/D D. Coloring Brackets time limit per test2 secon ...
- CF 149D Coloring Brackets 区间dp ****
给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2.每对括号必须只能给其中的一个上色 3.相邻的两个不能上同色,可以都不上色 求0-len-1这一区间内 ...
- Codeforces149D - Coloring Brackets(区间DP)
题目大意 要求你对一个合法的括号序列进行染色,并且需要满足以下条件 1.要么不染色,要么染红色或者蓝色 2.对于任何一对括号,他们当中有且仅有一个被染色 3.相邻的括号不能染相同的颜色 题解 用区间d ...
- codeforce 149D Coloring Brackets 区间DP
题目链接:http://codeforces.com/problemset/problem/149/D 继续区间DP啊.... 思路: 定义dp[l][r][c1][c2]表示对于区间(l,r)来说, ...
- CodeForces 149D Coloring Brackets 区间DP
http://codeforces.com/problemset/problem/149/D 题意: 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2 ...
- CF149D Coloring Brackets
CF149D Coloring Brackets Link 题面: 给出一个配对的括号序列(如"\((())()\)"."\(()\)"等, "\() ...
- Codeforces 508E Arthur and Brackets 区间dp
Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...
随机推荐
- CodePen 作品秀:Canvas 粒子效果文本动画
作品名称——Shape Shifter,基于 Canvas 的粒子图形变换实验.在页面下方的输入框输入文本,上面就会进行变换出对应的粒子效果文本动画. CodePen 作品秀系列向大家展示来自 Cod ...
- 移动端-js触摸事件
开发者工具 在移动开发中,一种较为容易的做法是,先在桌面上开始原型设计,然后再在打算要支持的设备上处理移动特有的部分.多点触摸正是难以在PC上进行测试的那些功能之一,因为大部分的PC都没有触摸输入. ...
- 通过ABAP的标准功能,寻找源代码中的字符串
程序名称: *ABAP_SOURCE_SCAN 查找 作用:通过这个标准程序,可以在系统的源代码中进行字符串的查找!
- putty不能连接linxu,报:connection refused
用putty连接新装的centos6.5,出现了connection refused问题, 后参考http://blog.sina.com.cn/s/blog_60d2d62a0100tq2l.htm ...
- [原] SharePoint 2010 WebPart与Google地图系列 一:创建显示地图的WebPart
摘要: 作为信息化先驱的产品SharePoint 2010竟然对GIS相关技术支持如此有限,试问现在哪个企业没有大量的项目需要结合Google地图来进行开发,单纯地从Google Javascript ...
- Oracle11g 统计信息——统计信息自动收集任务
参考文献: Oracle11g 统计信息(一)-----统计信息自动收集任务 背景: 在使用cacti监控oracle数据库IO的时候发现每天晚上10点钟的时候oracle数据库读写明显增加,如下图所 ...
- JavaScript学习11 数组排序实例
JavaScript学习11 数组排序实例 数组声明 关于数组对象的声明,以前说过:http://www.cnblogs.com/mengdd/p/3680649.html 数组声明的一种方式: va ...
- Android 手机卫士--弹出对话框
在<Android 手机卫士--解析json与消息机制发送不同类型消息>一文中,消息机制发送不同类型的信息还没有完全实现,在出现异常的时候,应该弹出吐司提示异常,代码如下: private ...
- Android Studio教程--Android项目分享到Github
首先下载安装git 下载地址:https://git-scm.com/ 打开AS,并设置如下: 到github上面注册一个帐号 运行--cmd cd C:\Program Files\Git\bin ...
- android MediaPlayer API大全已经方法详解(转载)
通过这张图,我们可以知道一个MediaPlayer对象有以下的状态: 1)当一个MediaPlayer对象被刚刚用new操作符创建或是调用了reset()方法后,它就处于Idle状态.当调用了rele ...