CF149D 区间dp
http://codeforces.com/problemset/problem/149/D
2 seconds
256 megabytes
standard input
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).
The first line contains the single string s (2 ≤ |s| ≤ 700)
which represents a correct bracket sequence.
Print the only number — the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007(109 + 7).
(())
12
(()())
40
()
4
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 区间dp
题目大意:给定一个有效的括号序列对于每个括号有三种涂色方法,涂红色或蓝色或不涂。而且相邻的两个括号不能涂同样的颜色(能够都不涂)
对于每一对括号都要恰有一个括号涂色,问对于整个序列有多少涂色的方法
解题思路:dp[i][j][x][y]表示对于区间(i,j)左括号为x色,右括号为y色,有多少中情况。 对于区间(ij)若i和j是相应则转移到(i+1,j-1)若不正确应则转移到(i,p)*(p+1,j)当中p为i括号的相应点,详细转移请看代码
*/
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long LL;
const LL mod=1e9+7;
char a[800];
int n,Hash[800],tmp[800];
LL dp[705][705][4][4]; void dfs(int l,int r)
{
if(l+1==r)
{
dp[l][r][0][1]=1;
dp[l][r][1][0]=1;
dp[l][r][2][0]=1;
dp[l][r][0][2]=1;
return;
}
if(Hash[r]==l)
{
dfs(l+1,r-1);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i!=1)
dp[l][r][1][0]=(dp[l][r][1][0]+dp[l+1][r-1][i][j])%mod;
if(j!=1)
dp[l][r][0][1]=(dp[l][r][0][1]+dp[l+1][r-1][i][j])%mod;
if(i!=2)
dp[l][r][2][0]=(dp[l][r][2][0]+dp[l+1][r-1][i][j])%mod;
if(j!=2)
dp[l][r][0][2]=(dp[l][r][0][2]+dp[l+1][r-1][i][j])%mod;
}
}
}
else
{
int p=Hash[l];
dfs(l,p);
dfs(p+1,r);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for(int x=0;x<3;x++)
{
for(int y=0;y<3;y++)
{
if(!(x==1&&y==1||x==2&&y==2))
dp[l][r][i][j]=(dp[l][r][i][j]+(dp[l][p][i][x]*dp[p+1][r][y][j])%mod)%mod;
}
}
}
}
}
}
int main()
{
while(~scanf("%s",a+1))
{
n=strlen(a+1);
int k=0;
memset(tmp,0,sizeof(tmp));
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
if(a[i]=='(')
{
tmp[k++]=i;
}
else
{
Hash[i]=tmp[k-1];
Hash[tmp[k-1]]=i;
k--;
}
}
dfs(1,n);
LL ans=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
ans=(ans+dp[1][n][i][j])%mod;
}
}
printf("%lld\n",ans);
}
return 0;
}
CF149D 区间dp的更多相关文章
- CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- BZOJ1055: [HAOI2008]玩具取名[区间DP]
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1588 Solved: 925[Submit][Statu ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
- BZOJ 1260&UVa 4394 区间DP
题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...
- 区间dp总结篇
前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
随机推荐
- day63-webservice 07.07.如何修改cxf配置文件路径
为什么第一次访问http://localhost:8080/cxf-web-server/service有点慢?证明第一次访问的时候CXFServlet被初始化了被加载了.一般是让CXFServlet ...
- [ASPX] 模版引擎XTemplate与代码生成器XCoder(源码)
模版引擎XTemplate是一个仿T4设计的引擎,功能上基本与T4一致(模版语法上完全兼容T4,模版头指令部分兼容). 自己设计模版引擎,就是为了代码生成器.网站模版.邮件模版等多种场合,也就是要能拿 ...
- [Java] Oracle的JDBC驱动的版本说明
classes12.jar,ojdbc14.jar,ojdbc5.jar和ojdbc6.jar的区别,之间的差异 作者:赵磊 博客:http://elf8848.iteye.com 来源:http:/ ...
- css3 animate基本属性
Css3animate属性 属性 描述 Css Animation 所有动画属性的简写属性,除了animation-play-state属性 Animation:name duration timin ...
- RDS中的.frm和.ibd文件转换为sql文件
--- 转自他人 mysql存储在磁盘中,各种天灾人祸都会导致数据丢失.大公司的时候我们常常需要做好数据冷热备,对于小公司来说要做好所有数据备份需要支出大量的成本,很多公司也是不现实的.万一还没有做好 ...
- ObjecT4:On-line multiple instance learning (MIL)学习
原文链接:http://blog.csdn.net/ikerpeng/article/details/19235391 用到论文,直接看翻译. 文章:Robust object tracking wi ...
- mongodb报错一例
开发程序报错信息: Caused by: com.mongodb.MongoException: Executor error: OperationFailed: Sort operation use ...
- 优动漫PAINT动画创作功能介绍
优动漫PAINT也就是我们常说的clip studio paint(CSP)的中文版本,它是一款功能强大的漫画.插画绘制软件,它搭载了绘制漫画和插画所需的所有功能,包括丰富的笔工具.超强的笔压感应和手 ...
- Java中的强制转换
特点: 1.需要程序员手动修改代码 2.语法:范围小的类型 变量名 = (范围小的类型)范围大的类型的数据 3.从范围小 到 范围大 注意: 强制类型转换可能会造成数据的丢失哦,小伙伴们在应用时一定 ...
- MongoDB_可视化工具Robo 3T
Robo 3T可以对MongoDB进行可视化操作. Robo 3T安装 官网下载地址:https://robomongo.org/ 进入官网,点击下载,Studio 3T功能更全面,基础功能是免费的, ...