CodeForces 149D Coloring Brackets (区间DP)
题意:
给一个合法的括号序列,仅含()这两种。现在要为每对括号中的其中一个括号上色,有两种可选:蓝or红。要求不能有两个同颜色的括号相邻,问有多少种染色的方法?
思路:
这题的模拟成分比较多吧?两种颜色还有无色,用2个bit就可以表示了。然后就是各种转移,注意结果可能非常大,要取模后输出。转移主要是不让同颜色的括号在一起。处理时可以用DFS,在区间[L,R]中找到距离最远的所有合法括号,递归往下处理子问题,直到剩下一对括号直接处理就行了。
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define INF 0x3f3f3f3f
#define LL unsigned long long
using namespace std;
const double PI = acos(-1.0);
const int N=;
const int mod=1e9+;
char s[N]; LL dp[N][N][][]; //0无色,1红色,2蓝色 void cal(int ll,int mid,int rr) //区间合并
{
if(ll==mid) return ;
for(int i=; i<; i++)
for(int j=; j<; j++)
for(int k=; k<; k++)
for(int u=; u<; u++)
if(k!=u||k==)
dp[ll][rr][i][j]+=(dp[ll][mid-][i][k] * dp[mid][rr][u][j])%mod;
}
void cal2(int ll,int rr) //区间嵌套的
{
for(int k=; k<; k++) //枚举内区间
for(int u=; u<; u++)
{
if( u!= ) dp[ll][rr][][]=(dp[ll][rr][][]+dp[ll+][rr-][k][u])%mod;
if( u!= ) dp[ll][rr][][]=(dp[ll][rr][][]+dp[ll+][rr-][k][u])%mod;
if( k!= ) dp[ll][rr][][]=(dp[ll][rr][][]+dp[ll+][rr-][k][u])%mod;
if( k!= ) dp[ll][rr][][]=(dp[ll][rr][][]+dp[ll+][rr-][k][u])%mod;
}
} void DFS(int L,int R)
{
if(L+==R)
{
dp[L][R][][]=dp[L][R][][]=;
dp[L][R][][]=dp[L][R][][]=;
return ;
}
int q=L+;
while( q<R ) //将[L->R]分成多个子区间
{
int p=q, cnt=;
while( ) //找对应的另一半括号)的位置
{
if( s[q]=='(' ) cnt++;
if( s[q]==')' ) cnt--;
if(cnt==) break;
q++;
}
DFS(p, q); //计算区间[p->q]
cal(L+, p, q); //两个区间合并
q++;
}
cal2(L,R); //子区间并到大区间
} int main()
{
//freopen("input.txt", "r", stdin);
memset(dp,,sizeof(dp));
scanf("%s",s+);
int n=strlen(s+);
DFS(, n+); //将s[0]和s[n+1]视为一对括号比较好处理
LL ans=; for(int i=; i<; i++)
for(int j=; j<; j++)
ans=(ans+dp[][n][i][j])%mod; cout<<ans<<endl;
return ;
}
AC代码
CodeForces 149D Coloring Brackets (区间DP)的更多相关文章
- codeforces 149D Coloring Brackets (区间DP + dfs)
题目链接: codeforces 149D Coloring Brackets 题目描述: 给一个合法的括号串,然后问这串括号有多少种涂色方案,当然啦!涂色是有限制的. 1,每个括号只有三种选择:涂红 ...
- CodeForces 149D Coloring Brackets 区间DP
http://codeforces.com/problemset/problem/149/D 题意: 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2 ...
- codeforce 149D Coloring Brackets 区间DP
题目链接:http://codeforces.com/problemset/problem/149/D 继续区间DP啊.... 思路: 定义dp[l][r][c1][c2]表示对于区间(l,r)来说, ...
- CF 149D Coloring Brackets 区间dp ****
给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2.每对括号必须只能给其中的一个上色 3.相邻的两个不能上同色,可以都不上色 求0-len-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 ...
- 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)
题目链接 Coloring Brackets 考虑树型DP.(我参考了Q巨的代码还是略不理解……) 首先在序列的最外面加一对括号.预处理出DFS树. 每个点有9中状态.假设0位不涂色,1为涂红色,2为 ...
- CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...
- CodeForces 149D Coloring Brackets
Coloring Brackets time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...
- Codeforces - 149D 不错的区间DP
题意:有一个字符串 s. 这个字符串是一个完全匹配的括号序列.在这个完全匹配的括号序列里,每个括号都有一个和它匹配的括号 你现在可以给这个匹配的括号序列中的括号染色,且有三个要求: 每个括号只有三种情 ...
随机推荐
- 用于生成交易统计时间戳(常配合echarts走势图使用)
<?php //获取交易统计时间戳 时间段内每小时 public function getPayCountTimeHours($start_date,$end_date){ $data = ar ...
- 使用 DDMenuController 类的方法(非常好用的抽屉类)
关于使用 DDMenuController 类的方法笔记:参考 DDMenuController 是一款非常好用的抽屉类文件. #pragma mark - 界面配置左右导航条的按钮 //[self ...
- Unity4.0配置
关于Unity4.0的使用: 一 安装Unity 在程序包管理器控制台输入命令:Istall-Pckage unity.mvc安装后会在App_Start中生成UnityConfig.cs 和Unit ...
- virtualBox中的centOS虚拟机硬盘扩容
1. 在virtualBox中给虚拟机添加虚拟硬盘 此时. 已经将yanwu_disk1.vdi 虚拟硬盘添加到了虚拟机中, 接下来就是进行硬盘的挂载 https://www.cnblogs.com/ ...
- git搭建私有仓库
git gui参考 https://ask.helplib.com/git/post_1004941
- 洛谷 - P2568 - GCD - 欧拉函数
https://www.luogu.org/problemnew/show/P2568 统计n以内gcd为质数的数的个数. 求 \(\sum\limits_p \sum\limits_{i=1}^{n ...
- 豆瓣api获取图片403
1.问题描述 豆瓣的图片资源在网页中不能正常显示,403禁止访问,把地址放到浏览器中就可以正常访问了? 原因是豆瓣现在有防盗链 2.解决问题 在页面中加上 <meta name="re ...
- Cannot set headers after they are sent to the client
D:\le\node_modules\mysql\lib\protocol\Parser.js: throw err; // Rethrow non-MySQL errors ^ Error [ERR ...
- HDU2896【AC自动机-模板】
思路: 因为不同病毒特征码不会相同. AC自动机,然后对于每一个输出即可. 注意:以上字符串中字符都是ASCII码可见字符(不包括回车);G++ MLE. //#include <bits/st ...
- 玲珑学院1072 【DFS】
蛤蛤,略蠢. priority_queue 自定义优先级 和排序是反的 struct node { int x,y; friend bool operator< (node a,node b) ...