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


区间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. JavaSript模块规范 - AMD规范与CMD规范介绍 (转载lovenyf.blog.chinaunix.net)

    JavaSript模块化   在了解AMD,CMD规范前,还是需要先来简单地了解下什么是模块化,模块化开发?       模块化是指在解决某一个复杂问题或者一系列的杂糅问题时,依照一种分类的思维把问题 ...

  2. 微信不支持Object.assign

    微信不支持Object.assign,让我Vue怎么用QAQ... 解决方法: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Refe ...

  3. ORA-00054:资源正忙,但指定以nowait方式

    PL/SQL执行SQL脚本文件,报错如下: 百度寻找答案,认为是被锁了. select session_id from v$locked_object; 结果没有任何数据.   后来把PL/SQL关闭 ...

  4. Failed to connect to database. Maximum number of conections to instance exceeded

    我们大体都知道ArcSDE的连接数有 48 的限制,很多人也知道这个参数可以修改,并且每种操作系统能支持的最大连接数是不同的. 如果应用报错:超出系统最大连接数 该如何处理? 两种解决办法: 第一,首 ...

  5. Vault插件示例--Vault Explorer与Thin Client的集成。

    Autodesk Vault 2014的Subscription 包中有一个组件叫做Thin Client.这个瘦客户端有着全新的界面,又给了我们一个全新的选择.ThinClient实际是在Vault ...

  6. Android EditText输入最大值提示功能

    public class LengthFilter implements InputFilter { public LengthFilter(int max) { mMax = max; } @Ove ...

  7. XCode设置(怎么让代码收缩)

    有时候刚使用一台电脑 可能会没有代码收缩的功能. 在哪里设置呢?看图 打开xcode 的 偏好设置 找到textEditing 把Code folding勾选上 就可以了

  8. UI控件(复习一下)

    如何修改控件状态• 可见,确实需要经常修改控件状态• 那如何去修改控件的状态呢?方法很简单➢ 每一个UI控件都是一个对象➢ 修改UI控件的状态,其实就是修改控件对象的属性➢ 比如修改UILabel显示 ...

  9. 关于input全选反选恶心的异常情况

    上一篇讲到:第一次点击全选按钮input显示对勾,第二次则不显示,需要用prop来添加checked属性. 但是用prop会出现一个问题,对勾显示,而checked属性不会被添加(比如:$(" ...

  10. JS中的事件

    事件中的几种实现方式 Dom0时代 1.直接在html的属性中写JS代码 <div onclick="alert(4);">Div1 Element</div&g ...