Coloring Brackets
题意:
给一匹配的括号序列,要求每对括号恰有一个被染成蓝或红色,要求相邻的括号不同色,求方案数。
解法:
类比树的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的更多相关文章
- 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 Round #106 (Div. 2) D. Coloring Brackets 区间dp
题目链接: http://codeforces.com/problemset/problem/149/D D. Coloring Brackets time limit per test2 secon ...
- Codeforces 149D Coloring Brackets(树型DP)
题目链接 Coloring Brackets 考虑树型DP.(我参考了Q巨的代码还是略不理解……) 首先在序列的最外面加一对括号.预处理出DFS树. 每个点有9中状态.假设0位不涂色,1为涂红色,2为 ...
- 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 + dfs)
题目链接: codeforces 149D Coloring Brackets 题目描述: 给一个合法的括号串,然后问这串括号有多少种涂色方案,当然啦!涂色是有限制的. 1,每个括号只有三种选择:涂红 ...
- CF 149D Coloring Brackets(区间DP,好题,给配对的括号上色,求上色方案数,限制条件多,dp四维)
1.http://codeforces.com/problemset/problem/149/D 2.题目大意 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色, ...
- CF149D Coloring Brackets
CF149D Coloring Brackets Link 题面: 给出一个配对的括号序列(如"\((())()\)"."\(()\)"等, "\() ...
- Coloring Brackets (区间DP)
Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a soluti ...
- CF 149D Coloring Brackets 区间dp ****
给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2.每对括号必须只能给其中的一个上色 3.相邻的两个不能上同色,可以都不上色 求0-len-1这一区间内 ...
随机推荐
- 苦逼IT才能看懂的笑话
这是苦逼IT才能看懂的笑话1.栈和队列的区别是啥? 吃多了拉就是队列:吃多了吐就是栈 2.世界上最遥远的距离不是生与死,而是你亲手制造的BUG就在你眼前,你却怎么都找不到她... 3.<c++程 ...
- 【Sprint3冲刺之前】项目完成时间表
Sprint2冲刺成果——项目完成时间表 项目完成时间表 经过Sprint2小组总结会议的探讨,我们决定一起约定项目alpha版本,beta版本,release版本的发布时间,发布形式及预计完成效果 ...
- linux下nginx+php+mysql 自助环境搭建
++++++++++++++++++++++++++++++++++++++++++++++linux下nginx+php+mysql环境搭建+++++++++++++++++++++++++++++ ...
- Unity3D总结:关于射线碰撞
方法一:Physics.Raycast 光线投射 1.static function Raycast (origin : Vector3, direction : Vector3, distance ...
- 使用Auto Layout处理比例间距问题
使用Auto Layout处理比例间距问题 Auto Layout 是一个掌握起来很具有挑战性的东西.iOS 9引入的 Stack Views和 layout 锚点有一些帮助,但是明白如何创建特定的 ...
- fabric-ca安装
1.Go版本1.7+(具体可参考Linux安装Go语言) 2.GOPATH环境变量正确配置 export GOROOT=/usr/local/go export GOPATH=/opt/gopath ...
- iOS开发- OpenGL ES屏幕截图
之前写过一个常规的屏幕截图:http://blog.csdn.net/hitwhylz/article/details/17189351 可是发现这个办法对于OpenGL 无用. 获取到的数据为空. ...
- LeetCode(70)题解: climbing-stairs
https://leetcode.com/problems/climbing-stairs/ 题目: You are climbing a stair case. It takes n steps t ...
- multiTarget within one project pods manage
step1:file->new->target create 1 targetstep2:change Podfile and update podstep3:check new targ ...
- # Playables API(翻译)
The Playables API provides a way to create tools, effects or other gameplay mechanisms by organi ...