题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数


区间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);
}
D. Coloring Brackets
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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).

Input

The first line contains the single string s (2 ≤ |s| ≤ 700) which represents a correct bracket sequence.

Output

Print the only number — the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007(109 + 7).

Examples
input
(())
output
12
input
(()())
output
40
input
()
output
4
Note

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 !]的更多相关文章

  1. 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 ...

  2. codeforces 149D Coloring Brackets (区间DP + dfs)

    题目链接: codeforces 149D Coloring Brackets 题目描述: 给一个合法的括号串,然后问这串括号有多少种涂色方案,当然啦!涂色是有限制的. 1,每个括号只有三种选择:涂红 ...

  3. Codeforces Round #106 (Div. 2) D. Coloring Brackets 区间dp

    题目链接: http://codeforces.com/problemset/problem/149/D D. Coloring Brackets time limit per test2 secon ...

  4. CF 149D Coloring Brackets 区间dp ****

    给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2.每对括号必须只能给其中的一个上色 3.相邻的两个不能上同色,可以都不上色 求0-len-1这一区间内 ...

  5. Codeforces149D - Coloring Brackets(区间DP)

    题目大意 要求你对一个合法的括号序列进行染色,并且需要满足以下条件 1.要么不染色,要么染红色或者蓝色 2.对于任何一对括号,他们当中有且仅有一个被染色 3.相邻的括号不能染相同的颜色 题解 用区间d ...

  6. codeforce 149D Coloring Brackets 区间DP

    题目链接:http://codeforces.com/problemset/problem/149/D 继续区间DP啊.... 思路: 定义dp[l][r][c1][c2]表示对于区间(l,r)来说, ...

  7. CodeForces 149D Coloring Brackets 区间DP

    http://codeforces.com/problemset/problem/149/D 题意: 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2 ...

  8. CF149D Coloring Brackets

    CF149D Coloring Brackets Link 题面: 给出一个配对的括号序列(如"\((())()\)"."\(()\)"等, "\() ...

  9. Codeforces 508E Arthur and Brackets 区间dp

    Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...

随机推荐

  1. 从0开始学angularjs-笔记03

    大家好,今天上班第一天,可能大家都不是很想上班吧,我也是一样啦---不想上班就来继续写我的angualrjs教程,造福大家吧!! 今天的主要讲解部分有以下几点:1.演示一个完整的项目结构  2.$sc ...

  2. jquery原型方法map的使用和源码分析

    原型方法map跟each类似调用的是同名静态方法,只不过返回来的数据必须经过另一个原型方法pushStack方法处理之后才返回,源码如下: map: function( callback ) { re ...

  3. npm 安装 ionic cordova

    针对npm安装 ionic 和 cordova 过程很慢,且有些安装文件被墙的问题,使用如下方式解决: 1)安装cnpm npm install -g cnpm 2)然后再使用cnpm 安装 ioni ...

  4. DevExpress 2015.2发布 看看有哪些更新

    下面是besy翻译的部分重要更新,要查看全部更新细节请访问英文官网. | 下载DevExpress 2015.2 Diagram Control 新的DevExpress Diagram Contro ...

  5. GitHub使用指南

    文章地址:http://www.worldhello.net/gotgithub/index.html

  6. Android studio 克隆分支

    1.当远程仓库有两个分支时,像这样的 获得分支的远程仓库地址 注意:这个地址默认就是克隆master 分支 2.如何克隆 v1.2 分支 ? 用android studio 把项目打开,可以看到 此时 ...

  7. Android SharedPreference的使用

    在<Android 在内部存储读写文件>一文中,谈到了登录用户名和密码的方法,通过读取内存文件来实现,但是会出现问题,因为登录名和密码的形式通过username##password的形式, ...

  8. mac 下如何切换jdk的版本

    1.打开.bash_profile文件添加一个函数 #add a function for switch idk version.function jdkset() { if [ $# -ne 0 ] ...

  9. js去掉字符串前后空格的五种方法

    转载 :http://www.2cto.com/kf/201204/125943.html 第一种:循环检查替换[javascript]//供使用者调用  function trim(s){  ret ...

  10. 关于tableview内cell自定义的注册以及创建

    自定义cell的方法主要有两种,storyboard以及xib(假设新建的是cellTableViewCell类) 比较倾向于xib方式使用xib在xib文件内将自定义的cell绘制好后导入到调用文件 ...