Coloring Brackets (区间DP)
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 modulo 1000000007 (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
(())
12
(()())
40
()
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.
题目大意:
给定一个合法的括号序列,每对括号有且只能涂一种颜色(一半红色或蓝色,一半不涂),且相邻的两个位置不能涂同一种颜色,求有多少种涂法。
dp[i][j][x][y]:i,j分别代表左右段点,x,y分别代表左右端点的颜色。
若当前的i,j是一对括号dp[i][j][][]则由dp[i+1][j-1][][]推来,反之,找到与i匹配的mid,dp[i][j][][]则由dp[i][mid][][]与dp[mid+1][j][][]推来。
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int MOD=1e9+;
ll dp[][][][];///左端点,右端点,左颜色,右颜色
int has[];
stack<int> st;
void dfs(int l,int r)
{
if(l==r) return;
if(l+==r)
{
dp[l][r][][]=;
dp[l][r][][]=;
dp[l][r][][]=;
dp[l][r][][]=;
return;
}
if(has[l]==r)
{
dfs(l+,r-);
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
if(j!=) dp[l][r][][]=(dp[l][r][][]+dp[l+][r-][i][j])%MOD;
if(i!=) dp[l][r][][]=(dp[l][r][][]+dp[l+][r-][i][j])%MOD;
if(j!=) dp[l][r][][]=(dp[l][r][][]+dp[l+][r-][i][j])%MOD;
if(i!=) dp[l][r][][]=(dp[l][r][][]+dp[l+][r-][i][j])%MOD;
}
}
return;
}
int mid=has[l];
dfs(l,mid);
dfs(mid+,r);
for(int i=;i<;i++)
for(int j=;j<;j++)
{
for(int k=;k<;k++)
for(int m=;m<;m++)
{
if(k==m&&k) continue;
dp[l][r][i][j]+=(dp[l][mid][i][k]*dp[mid+][r][m][j])%MOD;
}
dp[l][r][i][j]%=MOD;
}
return;
}
int main()
{
ios::sync_with_stdio(false);
string s;
cin>>s;
for(int i=;s[i];i++)///括号匹配
{
if(s[i]=='(')
st.push(i);
else
{
has[st.top()]=i;
has[i]=st.top();
st.pop();
}
}
dfs(,s.size()-);
ll ans=;
for(int i=;i<;i++)
for(int j=;j<;j++)
ans+=dp[][s.size()-][i][j];
cout<<ans%MOD<<'\n';
return ;
}
Coloring Brackets (区间DP)的更多相关文章
- CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-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 ...
- 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 ...
- Codeforces 508E Arthur and Brackets 区间dp
Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...
- POJ 2995 Brackets 区间DP
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...
随机推荐
- luogu P3371 & P4779 单源最短路径spfa & 最大堆优化Dijkstra算法
P3371 [模板]单源最短路径(弱化版) 题目背景 本题测试数据为随机数据,在考试中可能会出现构造数据让SPFA不通过,如有需要请移步 P4779. 题目描述 如题,给出一个有向图,请输出从某一点出 ...
- Java图解
java虚拟机 JVM运行过程: java开发工具包 java入门图解1 java入门图解2 java入门图解3 java入门图解4
- Java三大特性之---封装
封装从字面上来理解就是包装的意思,专业点就是信息隐藏,是指利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一个不可分割的独立实体,数据被保护在抽象数据类型的内部,尽可能地隐藏内部的细节,只保 ...
- VMwareworkstation 12安装
由于这篇博客中有大量截图,一个一个上传太累了,所以请点击以下链接进行查看 百度云:http://pan.baidu.com/s/1bQxPSi 这里直接粘贴文字可以,但是详细的截图贴不出来!
- zoj3772Calculate the Function(矩阵+线段树)
链接 表达式类似于斐波那契 但是多了一个变量 不能用快速幂来解 不过可以用线段树进行维护 对于每一个点够一个2*2的矩阵 1 a[i] 1 0 这个矩阵应该不陌生 类似于构造斐波那契的那个数列 ...
- qconshanghai2017
https://2017.qconshanghai.com/schedule 第一天 (2017/10/17 星期二) 时间 日程 07:45-09:00 签到 上午 主题演讲 软件质量优化与平台创新 ...
- Java GUI 基础组件
1.JLabel 标签 构造函数: JLabel() JLabel(String text) JLabel(String text,int align) //第二个参数设置文本的对齐方式,常 ...
- 4. iOS测试常用方法
1. [XCUIElement exists]方法只能确定这个View是否存在,即使不在当前屏幕上也返回True.如果要确定View是否在屏幕可见范围内,可以判断其Frame是否在Window的 ...
- 洛谷P2762 太空飞行计划问题(最大权闭合图)
题意 有$m$个实验,$n$中器材,每个实验需要使用一些器材 每个实验有收入,每个器材有花费 最大化收入 - 花费 Sol 最大权闭合图的经典应用 从$S$向每个实验连流量为该实验收入的边 从每个器材 ...
- PHP Deprecated: Function split() is deprecated in /var/www/html/cacti/cmd.php on line 61
[root@localhost cacti]# php cmd.php PHP Deprecated: Function split() is deprecated in /var/www/html/ ...