题意:

  给一个合法的括号序列,仅含()这两种。现在要为每对括号中的其中一个括号上色,有两种可选:蓝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)的更多相关文章

  1. codeforces 149D Coloring Brackets (区间DP + dfs)

    题目链接: codeforces 149D Coloring Brackets 题目描述: 给一个合法的括号串,然后问这串括号有多少种涂色方案,当然啦!涂色是有限制的. 1,每个括号只有三种选择:涂红 ...

  2. CodeForces 149D Coloring Brackets 区间DP

    http://codeforces.com/problemset/problem/149/D 题意: 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2 ...

  3. codeforce 149D Coloring Brackets 区间DP

    题目链接:http://codeforces.com/problemset/problem/149/D 继续区间DP啊.... 思路: 定义dp[l][r][c1][c2]表示对于区间(l,r)来说, ...

  4. CF 149D Coloring Brackets 区间dp ****

    给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2.每对括号必须只能给其中的一个上色 3.相邻的两个不能上同色,可以都不上色 求0-len-1这一区间内 ...

  5. Codeforces Round #106 (Div. 2) D. Coloring Brackets 区间dp

    题目链接: http://codeforces.com/problemset/problem/149/D D. Coloring Brackets time limit per test2 secon ...

  6. 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 ...

  7. Codeforces 149D Coloring Brackets(树型DP)

    题目链接 Coloring Brackets 考虑树型DP.(我参考了Q巨的代码还是略不理解……) 首先在序列的最外面加一对括号.预处理出DFS树. 每个点有9中状态.假设0位不涂色,1为涂红色,2为 ...

  8. CF149D. Coloring Brackets[区间DP !]

    题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...

  9. CodeForces 149D Coloring Brackets

    Coloring Brackets time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...

  10. Codeforces - 149D 不错的区间DP

    题意:有一个字符串 s. 这个字符串是一个完全匹配的括号序列.在这个完全匹配的括号序列里,每个括号都有一个和它匹配的括号 你现在可以给这个匹配的括号序列中的括号染色,且有三个要求: 每个括号只有三种情 ...

随机推荐

  1. 如何使用最简单的方法将一个已经存在的工程中使用 cocaPodfile

    在网上搜索的使用 cocaPods 安装一些优秀的框架,搜索的博客大多步骤都是非常的麻烦,这里的方法非常的简单,本篇仅仅作为以后备用.   第一步:首先找到我们的工程,在终端中输入 cd 拖入已经存在 ...

  2. 改变静态文本框和PictureControl的背景颜色

    /************************************************************************/ /* 改变静态文本框和选择框的背景颜色 */ /* ...

  3. 1.5-1.6 oozie部署

    一.部署 可参考文档:http://archive.cloudera.com/cdh5/cdh/5/oozie-4.0.0-cdh5.3.6/DG_QuickStart.html 1.解压oozie ...

  4. Laravel框架之Request操作

    public function request(Request $request){ //1.取值 //echo $request->input('name'); //echo $request ...

  5. linux tar命令及解压总结

    把常用的tar解压命令总结下,当作备忘: tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其 ...

  6. CodeForces - 357C Knight Tournament 伪并查集(区间合并)

    Knight Tournament Hooray! Berl II, the king of Berland is making a knight tournament. The king has a ...

  7. CodeForces - 566D Restructuring Company 并查集的区间合并

    Restructuring Company Even the most successful company can go through a crisis period when you have ...

  8. 甩掉 ashx/asmx,使用jQuery.ajaxWebService请求WebMethod,Ajax处理更加简练

    在WebForm下 开发ajax程序,需要借助于一般处理程序(*.ashx)或web服务(*.asmx),并且每一个ajax请求,都要建一个这样的文件,如此一来,如果在一个项目中ajax程序多了,势必 ...

  9. unity3d读写txt

    http://www.cnblogs.com/sunet/p/3851353.html?utm_source=tuicool 记录一下昨天用到的技术点:基于android平台unity3d读写txt. ...

  10. IT兄弟连 JavaWeb教程 Servlet会话跟踪 Session常用方法

    ●  public Object getAttribute(String name) 该方法返回在该session会话中具有指定名称的对象,如果没有指定名称的对象,则返回null. ●  public ...