codeforces 149D Coloring Brackets (区间DP + dfs)
题目链接:
codeforces 149D Coloring Brackets
题目描述:
给一个合法的括号串,然后问这串括号有多少种涂色方案,当然啦!涂色是有限制的。
1,每个括号只有三种选择:涂红色,涂蓝色,不涂色。
2,每对括号有且仅有其中一个被涂色。
3,相邻的括号不能涂相同的颜色,但是相邻的括号可以同时不涂色。
解题思路:
这个题目的确是一个好题,无奈我太蠢,读错题意。以为(())这样的括号序列在涂色的时候,第一个括号与第三个括号也可以看做是一对。这样的话还要统计合法的括号匹配数目,还要计算涂色的方案。然后想了好久好久。最后发现并不是这样的,给出的括号序列,括号的匹配都是固定的,也就是说只需要给这些固定匹配的括号按照上面限制涂色就好啦。
可以定义 dp[l][r][x][y] 表示区间 [l, r] 在左端点涂x色,右端点涂y色的情况下的方案数。其中0代表不涂色, 1代表涂红色, 2代表涂蓝色。
窝们可以把括号序列可以分为两类分别进行状态转移:
括号套括号,状态转移是:dp[l][r][x][y] += dp[l+1][r-1][x'][y'] (0<=x'<3, x'!=x, 0<=y'<3, y!=y')
多个匹配串连起来,转台转移是:dp[l][r][x][y] += dp[l][nu][x'][y'] * dp[nu][r][x''][y''] (nu是l对应的另一边括号)
当l+1 == r的时候有:dp[l][r][0][1] = 1; dp[l][r][1][0] = 1;
dp[l][r][0][2] = 1; dp[l][r][2][0] = 1;
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef __int64 LL;
const LL mod = ;
const LL maxn = ;
LL dp[maxn][maxn][][], match[maxn], tep[maxn]; void get_macth (char a[])
{
int p = ;
for (int i=; a[i]; i++)
{
if (a[i] == '(')
tep[p ++] = i;
else
{
match[i] = tep[p-];
match[tep[p-]] = i;
p --;
}
}
} void dfs (int l, int r)
{
if (l + == r)
{
dp[l][r][][] = ;
dp[l][r][][] = ;
dp[l][r][][] = ;
dp[l][r][][] = ;
return ;
} else if (match[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 ;
} else
{
int nu = match[l];
dfs (l, nu);
dfs (nu+, r); for (int i=; i<; i++)
for (int j=; j<; j++)
for (int x=; x<; x++)
for (int y=; y<; y++)
if (!(x==&&y== || x==&&y==))
dp[l][r][i][j] = (dp[l][r][i][j] + dp[l][nu][i][x]*dp[nu+][r][y][j]) % mod;
} } int main ()
{
char s[maxn]; while (scanf ("%s", s) != EOF)
{
memset(dp, , sizeof(dp));
int len = strlen (s);
get_macth (s);
dfs (, len-); LL ans = ;
for (int i=; i<; i++)
for (int j=; j<; j++)
ans = (ans + dp[][len-][i][j]) % mod;
printf ("%I64d\n", ans); }
return ;
}
codeforces 149D Coloring Brackets (区间DP + dfs)的更多相关文章
- 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为 ...
- CodeForces 149D Coloring Brackets
Coloring Brackets time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...
- CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...
- Codeforces149D - Coloring Brackets(区间DP)
题目大意 要求你对一个合法的括号序列进行染色,并且需要满足以下条件 1.要么不染色,要么染红色或者蓝色 2.对于任何一对括号,他们当中有且仅有一个被染色 3.相邻的括号不能染相同的颜色 题解 用区间d ...
随机推荐
- 格式转换至yuv422转 yuv420
//pYUV为422,yuv为420 /*ok! * brief:pyuv is yuv422sp srcIn, and yuv is yuv420p desOut */ int YUV422To4 ...
- UVA - 11019 Matrix Matcher hash+KMP
题目链接:传送门 题解: 枚举每一行,每一行当中连续的y个我们hash 出来 那么一行就是 m - y + 1个hash值,形成的一个新 矩阵 大小是 n*(m - y + 1), 我们要找到x*y这 ...
- hihocode #1388 : Periodic Signal NTT
#1388 : Periodic Signal 描述 Profess X is an expert in signal processing. He has a device which can ...
- poj3349(hash or violence)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 38600 Accep ...
- Spring的声明式事务
1.与hibernate集成 <bean id="sessionFactory" class="org.springframework.orm.hibernate3 ...
- SQL Server 2012 安装图解教程(附sql2012下载地址)
在安装微软最新数据库SQL Server 2012之前,编者先确定一下安装环境:Windonws 7 SP1,32位操作系统.CPU是2.1GHz赛扬双核T3500,内存2.93GB. sql2012 ...
- ubuntu12.04配置NFS服务详解
1:安装nfs sudo apt-get install nfs-kernel-server 2:配置服务 sudo vim /etc/exports 在末尾添加 /home/jyg *(rw,syn ...
- Silverlight结合Web Service进行文件上传
search了非常多的文章,总算勉强实现了.有许多不完善的地方. 在HCLoad.Web项目下新建目录Pics复制一张图片到根目录下. 图片名:Bubble.jpg 右击->属性->生成操 ...
- Java中的ArrayList
ArrayList是所谓的动态数组.用一个小例子: import java.util.ArrayList; import java.util.Iterator; import java.util.Li ...
- 【NOIP2012】 国王游戏
[题目链接] 点击打开链接 [算法] 按ai * bi升序排序,贪心即可 [代码] #include<bits/stdc++.h> using namespace std; #define ...