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来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
随机推荐
- 【BZOJ3926】【ZJOI2015】诸神眷顾的幻想乡 广义后缀自动机
题目: 题目在这里 思路&做法: 参考的题解 既然只有\(20\)个叶子节点, 那么可以从每个叶子节点往上建一颗\(trie\)树, 然后合并成一棵大的\(trie\)树, 然后构建广义后缀自 ...
- 负载均衡获得真实源IP的6种方法
除了X-FORWARD-FOR,负载均衡中获得真实源IP的方法还有很多种. 本文抛砖引玉,主要介绍获得真实源IP的多种方法,而不是具体配置. 负载均衡获得真实IP的方法有很多种,将形成专题文章. 本文 ...
- TensorFlow——分布式的TensorFlow运行环境
当我们在大型的数据集上面进行深度学习的训练时,往往需要大量的运行资源,而且还要花费大量时间才能完成训练. 1.分布式TensorFlow的角色与原理 在分布式的TensorFlow中的角色分配如下: ...
- win7如何给虚拟机设置共享文件
友情提示:设置之前先把虚拟机关掉 1. 安装vmtools 安装过的,则不需要 重新安装 如果没有安装vmware tools,点击安装(需要联网下载) ,下载完成后,打开虚拟机 点击安装,安装完毕后 ...
- 如何用jQuery实现div随鼠标移动而移动?(详解)----2017-05-12
重点是弄清楚如何获取鼠标现位置与移动后位置,div现在位置与移动后位置: 用jQuery实现div随鼠标移动而移动,不是鼠标自身的位置!!而是div相对于之前位置的移动 代码如下:(注意看绿色部分的解 ...
- 用Webpack构建Vue项目
开始之前,需要安装node环境.(安装过程在此就不啰嗦了) 1.创建基本结构 首先我们要创建一个空文件夹(我这里叫todos,你可以随便命名)作为项目的根目录. 创建一个没有任何依赖关系的pack ...
- Kettle 版本及使用问题
kettle 简介 Kettle也叫PDI (Pentaho Data Intergration) Kettle 版本及下载 7.1及更早版本: https://sourceforge.net/pro ...
- Centos7中 文件大小排序
centos7中根据文件大小排序以及jenkins配置每周删除一次jobs日志信息 https://blog.csdn.net/u013066244/article/details/70232050
- 错误信息:getOutputStream() has already been called for this response
原因(转): getOutputStream()和getWriter()这两个方法不能在一个请求内同时使用, 如果使用forward,这时将要跳转到的页面是要用getWriter()方法获得输出流把页 ...
- css3的过滤效果
上面的图片就是css3新特性的滤镜效果,学会了这些那么我们这群爱美爱帅的大web是不是就可以完美的用代码实现照片美化了捏~~ 好,咱们先把照片后面的白框实现, <style> #div1{ ...