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


区间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. Autodesk 最新开发技术研讨会 -8月22日-Autodesk北京办公室

    为了增进与广大中国地区Autodesk产品的二次开发人员的了解与互动,帮助中国地区的Autodesk产品二次开发人员了解Autodesk最新的二次开发技术动向,并获得Autodesk公司专业开发支持顾 ...

  2. Android 沉浸式状态栏 实现方式二 ( 更简单 )

    以前写过一个沉浸式状态栏 的实现方式 Android 沉浸式状态栏 实现方式一 现在有个更为简单的实现方式 . 相关链接 http://www.apkbus.com/forum.php?mod=vie ...

  3. Android自定义控件3--优酷菜单执行动画

    在上篇文章中实现了优酷菜单的布局,本文接着实现动画功能 本文地址:http://www.cnblogs.com/wuyudong/p/5914901.html,转载请注明源地址. 新建动画工具类Ani ...

  4. mvc上传到云虚拟机的问题解决

    我用vs2015写了个小网站,.Net Framework4.5. mvc 5,发布到本机iis上正常,在美橙申请了一个云虚拟机,发布过程中遇到的一些问题记录如下: 1.服务器支持的版本比较低 上传后 ...

  5. 自定义Dialog宽度占满屏幕

    一.自定义Dialog继承Dialog public class MyDialog extends Dialog { 二.为Dialog设置样式 在style中建立新样式继承 @android:sty ...

  6. MVC 生成图片,下载文件

    /// <summary> /// 生成图片 /// </summary> /// <param name="collection"></ ...

  7. ios 返回指定导航控制器

    UINavigationController *navigationVC = self.navigationController; NSMutableArray *viewControllers = ...

  8. MojoDatabase 源码学习之对象映射

    Mojo-database是我个人比较喜欢多开源项目,下文是该项目打介绍和地址: mojo-database 简介: MojoDatabase is an ActiveRecord-like ORM ...

  9. nodejs 小爬虫

    编写爬虫示例: var http = require('http'); var cheerio = require('cheerio'); var url = 'http://www.cnblogs. ...

  10. WEB核心IOC篇

    ioc概念的理解:(不是技术是一种设计思想) IOC (控制反转)     IoC(Inverse of Control)的字面意思是 控制反转 ,它包括两个内容:     其一是控制 (控制对象的实 ...