题意:

给一匹配的括号序列,要求每对括号恰有一个被染成蓝或红色,要求相邻的括号不同色,求方案数。

解法:

类比树的hash将括号序列转化为一棵树,树上子节点之间不得出现冲突,

子节点和父节点不得出现冲突,问题转化为树形dp问题,$O(n)$解决。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector> #define N 1000010
#define LL long long
#define P 1000000007LL using namespace std; /*
1 -> 10
2 -> 01
3 -> 20
4 -> 02
*/ int totn;
int next[N],q[N];
LL f[N][];
LL h[N][];
char S[N];
vector<int> G[N]; int build(int l,int r)
{
int x=++totn;
G[x].clear();
l++;
r--;
while(l<r)
{
G[x].push_back(build(l,next[l]));
l=next[l]+;
}
return x;
} void dp(int x)
{
int nl=G[x].size();
if(!nl)
{
for(int i=;i<;i++)
f[x][i]=;
return;
}
for(int i=;i<nl;i++) dp(G[x][i]);
for(int t=;t<;t++)
{
for(int i=;i<;i++) h[][i]=;
h[][t]=;
for(int i=;i<=nl;i++)
{
int p=G[x][i-];
LL sum = (h[i-][]+h[i-][]+h[i-][])%P;
h[i][] = sum * f[p][] % P;
h[i][] = sum * f[p][] % P; h[i][] = h[i-][]*f[p][]%P + h[i-][]*f[p][]%P;
if(h[i][]>=P) h[i][]-=P;
h[i][] += h[i-][]*f[p][]%P + h[i-][]*f[p][]%P;
if(h[i][]>=P) h[i][]-=P;
}
if(t==)
{
f[x][] = (h[nl][] + h[nl][] + h[nl][]) % P;
f[x][] = (h[nl][] + h[nl][])%P;
f[x][] = (h[nl][] + h[nl][])%P;
}
else if(t==) f[x][] = (h[nl][] + h[nl][] + h[nl][])%P;
else f[x][] = (h[nl][] + h[nl][] + h[nl][])%P;
}
} int n; int main()
{
freopen("test.txt","r",stdin);
while(~scanf("%s",S+))
{
S[]='(';
n=strlen(S);
S[n]=')';
S[n+]='\0';
n++;
int ed=;
totn=;
for(int i=;i<n;i++)
{
if(S[i]=='(') q[++ed]=i;
else
{
next[q[ed]]=i;
ed--;
}
}
build(,n-);
dp();
cout << f[][] << endl;
}
return ;
}
/*
(())
(()())
()
*/

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

  2. CodeForces 149D Coloring Brackets

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

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

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

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

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

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

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

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

  7. CF 149D Coloring Brackets(区间DP,好题,给配对的括号上色,求上色方案数,限制条件多,dp四维)

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

  8. CF149D Coloring Brackets

    CF149D Coloring Brackets Link 题面: 给出一个配对的括号序列(如"\((())()\)"."\(()\)"等, "\() ...

  9. Coloring Brackets (区间DP)

    Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a soluti ...

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

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

随机推荐

  1. Django开发微信公众平台

    处理微信发来的信息,实际上就是处理xml的过程.先写xml工具类 # -*- coding:utf-8 -*- from xml.dom import minidom from Web.model.W ...

  2. ffmpeg H264 编解码配置

    ffmpeg H264编解码前面有文章介绍下,本文主要介绍一些参数配置. 编码: int InitEncoderCodec( int iWidth, int iHeight) { AVCodec * ...

  3. 【Bootstrap】一个兼容IE8、谷歌等主流浏览器的受众门户式风格页面

    上一次写的<[Bootstrap]一个兼容IE8.谷歌等主流浏览器的受众巨幕式风格页面>(点击打开链接) 部分老一辈的需求可能对这样的后现代的风格并不惬意, 没关系,我们全然能够改变布局 ...

  4. http://www.cnblogs.com/yycxbjl/archive/2010/04/20/1716689.html

    http://www.cnblogs.com/yycxbjl/archive/2010/04/20/1716689.html PS: 开发工具 VS2010, 所有工程都为Debug状态,本人刚接触 ...

  5. GoogleFusionTablesAPI初探地图与云计算

    http://developer.51cto.com/art/200906/129324.htm http://yexiaochai.iteye.com/blog/1893735 http://yex ...

  6. cocos2dx-3.1 接入多盟广告sdk+Android (2)

    上篇,把基本工作做完了,接下来,在C++下加入代码. 1.打开win32下的project,加入一个类.DomobAd.代码例如以下, <pre name="code" cl ...

  7. 压力测试工具ab,wrk,locust简介

    ab 无疑是目前最常见的压力测试工具.其典型用法如下: shell> ab -k -c 100 -t 10 http://domain/path 其中,参数「c」表示的是并发, 参数「t」表示的 ...

  8. 在html中显示Flash的代码

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...

  9. python exception的传递

    try: block except1: except2: ... 如果block中出现了except,那么会先取匹配except1,如果匹配了,进行处理,程序继续执行. 如果except1没有匹配上, ...

  10. LESS和sa

    一. Sass/Scss.Less是什么? Sass (Syntactically Awesome Stylesheets)是一种动态样式语言,Sass语法属于缩排语法,比css比多出好些功能(如变量 ...