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这一区间内 ...
随机推荐
- oracle insert/update
insert into table (,,) values (,,) where update table set (,)=(select , from ) where
- final 和static
一.final 1.final变量: 当你在类中定义变量时,在其前面加上final关键字,那便是说,这个变量一旦被初始化便不可改变,这里不可改变的意思对基本类型来说是其值不可变,而对于对象变量来说其引 ...
- fabric-ca安装
1.Go版本1.7+(具体可参考Linux安装Go语言) 2.GOPATH环境变量正确配置 export GOROOT=/usr/local/go export GOPATH=/opt/gopath ...
- RestTemplate请求
JSONObject json = new JSONObject(sendParam);HttpHeaders headers = new HttpHeaders();MediaType type = ...
- 04 svn设置开机启动
一:设置开机启动[dos命令] 格式: sc create 服务名称 binPath=空格"安装目录/svnserve.exe -r 版本库地址目录 --service" star ...
- 【BZOJ1528】[POI2005]sam-Toy Cars 贪心
[BZOJ1528][POI2005]sam-Toy Cars Description Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Jasio ...
- 百度之星2016资格赛D,水题
很简单的题,主要是要用字符串哈希,把字符串处理成整数.接下来可以继续用hash,也可以像我一样用个map就搞定了. /* * Author : ben */ #include <cstdio&g ...
- Android笔记之为自定义对话框添加移动动画效果
给底部的对话框添加移动动画效果 可通过Window.setWindowAnimations(int resId)设置 SharingDialog.java package com.bu_ish.sha ...
- DuiLib笔记之Window常用属性
caption 可拖拽以移动窗口的标题区,类型:RECT.例如,要指定标题区高度为35,可设置caption="0,0,0,35" mininfo 窗口最小尺寸,类型:SIZE.例 ...
- LeetCode(83)Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For ex ...