Codeforces Round #106 (Div. 2) D. Coloring Brackets 区间dp
题目链接:
http://codeforces.com/problemset/problem/149/D
D. Coloring Brackets
time limit per test2 seconds
memory limit per test256 megabytes
#### 问题描述
> 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).
#### 输入
> 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).
####样例输入
> (()())
####样例输出
> 40
## 题意
> 每对括号必须满足一边染成红色或蓝色,另一边不染色,且相邻的两个括号颜色不同。
题解
之前的一种思路是dp[l][r][s1][s2]代表最左边的括号的状态为s1,s2但是,这种是错的额!因为你看不到r右边的限制了!!!!
贴个错误代码:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=777;
const int mod=1e9+7;
LL dp[maxn][maxn][3][3];
char str[maxn];
int rig[maxn];
int n;
LL dfs(int l,int r,int s1,int s2) {
if(l>=r) return 0;
if(l+1==r&&(s1==0&&s2>0||s1>0||s2==0)) return 1;
if(dp[l][r][s1][s2]>=0) return dp[l][r][s1][s2];
LL &res=dp[l][r][s1][s2]=0;
int ll=l,rr=rig[l];
LL cntl=0,cntr=0;
if(s1==0) {
if(ll+1<rr-1) {
cntl+=dfs(ll+1,rr-1,1,0);
cntl+=dfs(ll+1,rr-1,2,0);
cntl+=dfs(ll+1,rr-1,0,3-s2);
} else {
cntl=1;
}
cntl%=mod;
if(rr+1>r) {
cntr=1;
} else {
cntr+=dfs(rr+1,r,0,1);
cntr+=dfs(rr+1,r,0,2);
cntr+=dfs(rr+1,r,3-s2,0);
}
cntr%=mod;
res+=cntl*cntr;
res%=mod;
} else {
if(ll+1<rr-1) {
cntl+=dfs(ll+1,rr-1,3-s1,0);
cntl+=dfs(ll+1,rr-1,0,1);
cntl+=dfs(ll+1,rr-1,0,2);
} else {
cntl=1;
}
cntl%=mod;
if(rr+1>r) {
cntr=1;
} else {
cntr+=dfs(rr+1,r,0,1);
cntr+=dfs(rr+1,r,0,2);
cntr+=dfs(rr+1,r,1,0);
cntr+=dfs(rr+1,r,2,0);
}
cntr%=mod;
res+=cntl*cntr;
res%=mod;
}
return res;
}
int main() {
scf("%s",str+1);
n=strlen(str+1);
stack<int> mst;
clr(dp,-1);
clr(rig,-1);
for(int i=1; i<=n; i++) {
if(str[i]==')') {
rig[mst.top()]=i;
mst.pop();
} else {
mst.push(i);
}
}
// for(int i=1;i<=n;i++){
// prf("(%d,%d)\n",i,rig[i]);
// }
LL ans=dfs(1,n,0,1)+dfs(1,n,0,2)+dfs(1,n,1,0)+dfs(1,n,2,0);
bug(dp[1][n][0][1]);
bug(dp[1][n][0][2]);
bug(dp[1][n][1][0]);
bug(dp[1][n][2][0]);
bug(dp[2][5][2][0]);
prf("%I64d\n",ans);
return 0;
}
//end-----------------------------------------------------------------------
/*
(()())
*/
正确的表示应该是dp[l][r][s1][s2]代表l左边和r右边的限制!!!
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=777;
const int mod=1e9+7;
LL dp[maxn][maxn][3][3];
char str[maxn];
int rig[maxn];
int n;
LL dfs(int l,int r,int s1,int s2) {
if(l>r) return 1;
if(dp[l][r][s1][s2]>=0) return dp[l][r][s1][s2];
LL &res=dp[l][r][s1][s2]=0;
int ll=l,rr=rig[l];
if(rr==r){
if(s1==0){
if(s2==0){
res+=dfs(ll+1,rr-1,0,1);
res+=dfs(ll+1,rr-1,0,2);
res+=dfs(ll+1,rr-1,1,0);
res+=dfs(ll+1,rr-1,2,0);
res%=mod;
}else{
res+=dfs(ll+1,rr-1,0,3-s2);
res+=dfs(ll+1,rr-1,1,0);
res+=dfs(ll+1,rr-1,2,0);
res%=mod;
}
}else{
if(s2==0){
res+=dfs(ll+1,rr-1,0,1);
res+=dfs(ll+1,rr-1,0,2);
res+=dfs(ll+1,rr-1,3-s1,0);
res%=mod;
}else{
res+=dfs(ll+1,rr-1,0,3-s2);
res+=dfs(ll+1,rr-1,3-s1,0);
res%=mod;
}
}
}else{
if(s1==0){
res=(res+dfs(ll+1,rr-1,0,1)*dfs(rr+1,r,1,s2)%mod)%mod;
res=(res+dfs(ll+1,rr-1,0,2)*dfs(rr+1,r,2,s2)%mod)%mod;
res=(res+dfs(ll+1,rr-1,1,0)*dfs(rr+1,r,0,s2)%mod)%mod;
res=(res+dfs(ll+1,rr-1,2,0)*dfs(rr+1,r,0,s2)%mod)%mod;
}else{
res=(res+dfs(ll+1,rr-1,0,1)*dfs(rr+1,r,1,s2)%mod)%mod;
res=(res+dfs(ll+1,rr-1,0,2)*dfs(rr+1,r,2,s2)%mod)%mod;
res=(res+dfs(ll+1,rr-1,3-s1,0)*dfs(rr+1,r,0,s2)%mod)%mod;
}
}
return res;
}
int main() {
scf("%s",str+1);
n=strlen(str+1);
stack<int> mst;
clr(dp,-1);
clr(rig,-1);
for(int i=1; i<=n; i++) {
if(str[i]==')') {
rig[mst.top()]=i;
mst.pop();
} else {
mst.push(i);
}
}
LL ans=dfs(1,n,0,0);
prf("%I64d\n",ans);
return 0;
}
//end-----------------------------------------------------------------------
/*
(()())
*/
短一点的代码:
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=777;
const int mod=1e9+7;
LL dp[maxn][maxn][3][3];
char str[maxn];
int rig[maxn];
int n;
LL dfs(int l,int r,int s1,int s2) {
if(l>r) return 1;
if(dp[l][r][s1][s2]>=0) return dp[l][r][s1][s2];
LL &res=dp[l][r][s1][s2]=0;
int ll=l,rr=rig[l];
for(int i=1;i<=2;i++){
//left
if(i!=s1) res=(res+dfs(ll+1,rr-1,i,0)*dfs(rr+1,r,0,s2)%mod)%mod;
//right
if(rr<r||i!=s2) res=(res+dfs(ll+1,rr-1,0,i)*dfs(rr+1,r,i,s2)%mod)%mod;
}
return res;
}
int main() {
scf("%s",str+1);
n=strlen(str+1);
stack<int> mst;
clr(dp,-1);
clr(rig,-1);
for(int i=1; i<=n; i++) {
if(str[i]==')') {
rig[mst.top()]=i;
mst.pop();
} else {
mst.push(i);
}
}
LL ans=dfs(1,n,0,0);
prf("%I64d\n",ans);
return 0;
}
//end-----------------------------------------------------------------------
/*
(()())
*/
Codeforces Round #106 (Div. 2) D. Coloring Brackets 区间dp的更多相关文章
- 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 Round #369 (Div. 2) C. Coloring Trees(dp)
Coloring Trees Problem Description: ZS the Coder and Chris the Baboon has arrived at Udayland! They ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees (DP)
C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees(简单dp)
题目:https://codeforces.com/problemset/problem/711/C 题意:给你n,m,k,代表n个数的序列,有m种颜色可以涂,0代表未涂颜色,其他代表已经涂好了,连着 ...
- Codeforces Round #336 (Div. 2) D. Zuma(区间DP)
题目链接:https://codeforces.com/contest/608/problem/D 题意:给出n个宝石的颜色ci,现在有一个操作,就是子串的颜色是回文串的区间可以通过一次操作消去,问最 ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- codeforces 149D Coloring Brackets (区间DP + dfs)
题目链接: codeforces 149D Coloring Brackets 题目描述: 给一个合法的括号串,然后问这串括号有多少种涂色方案,当然啦!涂色是有限制的. 1,每个括号只有三种选择:涂红 ...
- CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...
- codeforce 149D Coloring Brackets 区间DP
题目链接:http://codeforces.com/problemset/problem/149/D 继续区间DP啊.... 思路: 定义dp[l][r][c1][c2]表示对于区间(l,r)来说, ...
随机推荐
- js array数组对象操作方法汇总
--------------------------更新自2018.6.11 js 数组对象操作方法如下: 1. 创建数组 var array1 = [1,2] //方法一 var array2 = ...
- python 3.x 循环读取文件用户登录
import os # 导入python自带库的模块 import sys # 导入python自带库的模块 import getpass # 导入python自带库的模块 lock_file = ' ...
- 关于Quartus+Modelsim 门级仿真 Warning (vopt-2216) Cannot find instance 'NA' specified in sdf.的解决办法
本文操作环境:Win 7 32位系统, Quartus II 11.1 ,Modelsim SE 10.1a 在Quartus II中调用Modelsim SE做Gate Level Simulait ...
- spark练习--由IP得到所在地
今天我们就来介绍,如何根据一个IP来求出这个IP所在的地址是什么,首先我们如果要做这个内容,那么我们要有一个IP地址的所在地字典,这个我们可以在网上购买,形如: 1.0.1.0|1.0.3.255|1 ...
- 2017-2018-2 20155315《网络对抗技术》Exp2:后门原理与实践
实验目的 学习建立一个后门连接. 教程 实验内容 使用netcat获取主机操作Shell,cron启动. 使用socat获取主机操作Shell, 任务计划启动. 使用MSF meterpreter(或 ...
- VBA中字符串连接/字符串拼接中“&”和“+”的区别
VBA中字符串连接/字符串拼接中“&”和“+”的区别 在VBA中用于字符串连接的只有“&”和“+”两种运算符. 1.“&”是强制性连接,就是不管什么都连接. 2.“+”是对 ...
- PostgreSQL集群方案相关索引页
磨砺技术珠矶,践行数据之道,追求卓越价值 返回顶级页:PostgreSQL索引页 本页记录所有本人所写的PostgreSQL的集群方案相关文摘和文章的链接: pgpool-II: 1 pgpool-I ...
- PyQt5用QTimer编写电子时钟
[说明] 本文用 PyQt5 的QTimer类的两种方式实现电子时钟 [效果图] [知识点] QTimer类提供了定时器信号/槽和单触发定时器. 它在内部使用定时器事件来提供更通用的定时器. QTim ...
- Java并发工具类(三):控制并发线程数的Semaphore
作用 Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源. 简介 Semaphore也是一个线程同步的辅助类,可以维护当前访问自身的线程个数 ...
- pycharm字体放大缩小设置
放大设置 File —> settings—> Keymap —>在搜寻框中输入:increase —> Increase Font Size(双击) —> 在弹出的对话 ...