题意:

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

解法:

类比树的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. 【每日Scrum】第五天(4.26) TD学生助手Sprint2站立会议

    站立会议 组员 昨天 今天 困难 签到 刘铸辉 (组长) 今天增加了几个页面的子菜单,然后设计了几个要用的界面 今天和楠哥做了课程事件和日历表操作的例子,并尝试做时间表和日历表的数据库设计 安卓的数据 ...

  2. HDMI速率计算

    我们在采集HDMI口的数据时,首先肯定要计算它的速率是多少.怎么计算这个速率,本文要跟大家分享的便是这个事情. HDMI口有三个TM-DS(Time Minimized Differential Si ...

  3. 零基础学python-4.5 标准类型分类

    1.按存储分:原子类型和容器类型 原子类型:仅仅能包括一个对象 容器类型:能够包括多个对象 分类 python类型 原子类型 数值和字符串 容器类型 列表.元组和字典 2.按能否够变化分:可变和不可变 ...

  4. Webkit JNI

    WebCoreFrameBridge.cpp BrowserFrame通过jni传下来的调用都会调用到WebCoreFrameBridge.cpp中的对应函数中,其他webkit的模块想回调信息给Br ...

  5. linux新建文件和文件夹命令

    1.touch命令 touch命令用来修改文件的访问时间.修改时间.如果没有指定时间,则将文件时间属性改为当前时间. 当指定文件不存在,touch命令变为创建该文件. 语法: touch [-acm] ...

  6. 在VC++空工程中使用MFC类,采用Unicode字符集后,运行工程程序报错的解决方案

    创建一个VC++空工程,将Project Properties->General->Use of MFC改为Use MFC in a Shared DLL 新建一个源文件,内容如下 #in ...

  7. ubuntu 查看网卡 数据包处理 速度

    ubuntu 查看网卡 数据包处理 速度 sar -l 1 10 首先要安装sar .使用 apt-get install atsar sar 命令中的 "-l"參数是 net-i ...

  8. mongodb学习之:安全和认证

    mongodb默认是不认证的,默认没有账号,只要能连接上服务就可以对数据库进行各种操作,mongodb认为安全最好的方法就是在一个可信的环境中运行它,保证之后可信的机器才能访问它.因此需要在登录的时候 ...

  9. github多用户提交错误Permission to repo denied to

    背景:同一台电脑的public key同时添加到了github的两个账户,导致user1的仓库没法正常提交. 解决办法:为两个账户分别配置ssh key,配置~/.ssh/config文件(windo ...

  10. pyinstaller-py2exe-cx_Freeze打包第一个wxPython程序HelloWorld

    pyinstaller 打包hello 7Mb ================= www.pyinstaller.org pip install pypiwin32 pip install pyin ...