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


区间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. about this

    var name="window name"; var obj={ name:"obj name", getNameFunc:function(){ //thi ...

  2. JavaScript强化教程——jQuery AJAX 实例

    什么是 AJAX?AJAX = 异步 JavaScript 和 XML(Asynchronous JavaScript and XML).简短地说,在不重载整个网页的情况下,AJAX 通过后台加载数据 ...

  3. [ javascript canvas isPointInPath(x,y) 判断点是否在最后绘制的图形中 ] javascript canvas isPointInPath(x,y) 判断点是否在最后绘制的图形中方法演示 效果之三

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  4. An interesting experiment on China’s censorship

    This paper presented a very interesting topic. Censorship in China has always drawn people's attenti ...

  5. Android 调用已安装市场,进行软件评分的功能实现

    Uri uri = Uri.parse("market://details?id="+getPackageName()); Intent intent = new Intent(I ...

  6. Android 开源框架Universal-Image-Loader完全解析(二)--- 图片缓存策略详解

    转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/26810303),请尊重他人的辛勤劳动成果,谢谢! 本篇文章 ...

  7. wifi强度数据采集器(android)

    来源:毕业设计 关键词:wifi数据的采集 SQLite数据库的使用 需求 采集实验室内各坐标处各wifi信号的强度 UI 因为是辅助工具,所以UI写的很简单,如下图 Wifi相关操作 //获取Wif ...

  8. django 基于proxy实现用户权限管理

    项目中经常会遇到用户权限管理的问题,django adminsite已经提供非常实用的用户权限管理机制.不过有些时候,我们希望根据相关用户属性来过滤adminsite中显示的内容.下文将结束如何实现: ...

  9. WPF学习之路(十二)控件(Range控件)

    ProgressBar 进度条,主要属性:Minimum\Maximun\Value, IsIndeterminate为True时,进度条会循环运转 <Grid> <Grid.Row ...

  10. jQuery中find和filter的区别

    本文来自:http://blog.csdn.net/woshixuye/article/details/7255260 这是jQuery里常用的2个方法. 他们2者功能是完全不同的,而初学者往往会被误 ...