Coloring Brackets

time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solution. Today you will face it.

You are given string s. It represents a correct bracket sequence. A correct bracket sequence is the sequence of opening ("(") and closing (")") brackets, such that it is possible to obtain a correct mathematical expression from it, inserting numbers and operators between the brackets. For example, such sequences as "(())()" and "()" are correct bracket sequences and such sequences as ")()" and "(()" are not.

In a correct bracket sequence each bracket corresponds to the matching bracket (an opening bracket corresponds to the matching closing bracket and vice versa). For example, in a bracket sequence shown of the figure below, the third bracket corresponds to the matching sixth one and the fifth bracket corresponds to the fourth one.

You are allowed to color some brackets in the bracket sequence so as all three conditions are fulfilled:

  • Each bracket is either not colored any color, or is colored red, or is colored blue.
  • For any pair of matching brackets exactly one of them is colored. In other words, for any bracket the following is true: either it or the matching bracket that corresponds to it is colored.
  • No two neighboring colored brackets have the same color.

Find the number of different ways to color the bracket sequence. The ways should meet the above-given conditions. Two ways of coloring are considered different if they differ in the color of at least one bracket. As the result can be quite large, print it modulo 1000000007 (109 + 7).

Input

The first line contains the single string s (2 ≤ |s| ≤ 700) which represents a correct bracket sequence.

Output

Print the only number — the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007 (109 + 7).

Examples

Input

(())

Output

12

Input

(()())

Output

40

Input

()

Output

4

Note

Let's consider the first sample test. The bracket sequence from the sample can be colored, for example, as is shown on two figures below.

The two ways of coloring shown below are incorrect.

题目大意:给定一个合法的括号序列,现在要你给括号序列染色。但是必须要满足如下条件:一、一个括号可以不染色,或者染红色,或者染蓝色;二、一对匹配的括号只能有一边染色(这里的匹配是唯一的对应,而不是只要是"()"就可,下同),且必须有一边染色;三、相邻的括号染的颜色必须不一样,但是可以都不染色。问你有多少种方案?因为方案数很大,所以结果模去1e9+7。

解题思路:容易看出这是一道DP题,并且是一道区间DP题。自己的DP很差,想了半天没想出来。于是去网上搜了一下别人的解法,瞬间恍然大悟了。设dp[l][r][x][y]表示区间[l,r]左端染的色是x,右端染的色是y的方案数,其中x,y取0,1,2,分别表示不染色,染红色,染蓝色。则该区间有三种情况,如下:

1、l+1==r,那么它们一定就是一对匹配的括号,此时,只可能有四种情况,方案数均为1,即:dp[l][r][0][1] = dp[l][r][1][0] = 1;dp[l][r][0][2] = dp[l][r][2][0] = 1;

2、l和r是一对匹配的括号,此时,区间被分为两部分,两端点以及区间[l+1,r-1],那么我们可以先算出区间[l+1,r-1]的方案数,再由此状态转移到当前区间,两端点情况也就四种,不冲突即可转移,详见代码;

3、l和r不是一对匹配的括号,此时,区间也可被分成两部分,区间[l,mid]和区间[mid+1,r],其中mid为l所对应与之匹配的括号,这样,一个合法的括号序列变成两个合法的括号序列,将它们分别求出方案数,再将不冲突的情况组合起来即可,详见代码。

附上AC代码:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = ;
const int mod = ;
ll dp[maxn][maxn][][];
string str;
stack<int> s;
map<int, int> pos; void get_match(){
for (int i=; i<str.size(); ++i){
if (str[i] == '(')
s.push(i);
else{
pos[i] = s.top();
pos[s.top()] = i;
s.pop();
}
}
} void dfs(int l, int r){
if (l+ == r){
dp[l][r][][] = dp[l][r][][] = ;
dp[l][r][][] = dp[l][r][][] = ;
return ;
}
if (pos[l] == r){
dfs(l+, r-);
for (int i=; i<; ++i)
for (int j=; j<; ++j){
if (j != )
dp[l][r][][] = (dp[l][r][][]+dp[l+][r-][i][j])%mod;
if (j != )
dp[l][r][][] = (dp[l][r][][]+dp[l+][r-][i][j])%mod;
if (i != )
dp[l][r][][] = (dp[l][r][][]+dp[l+][r-][i][j])%mod;
if (i != )
dp[l][r][][] = (dp[l][r][][]+dp[l+][r-][i][j])%mod;
}
return ;
}
int mid = pos[l];
dfs(l, mid);
dfs(mid+, r);
for (int i=; i<; ++i)
for (int j=; j<; ++j)
for (int k=; k<; ++k)
for (int s=; s<; ++s)
if (!(k==&&s==) && !(k==&&s==))
dp[l][r][i][j] = (dp[l][r][i][j]+dp[l][mid][i][k]*dp[mid+][r][s][j])%mod;
} int main(){
ios::sync_with_stdio(false);
cin.tie();
cin >> str;
get_match();
dfs(, str.size()-);
ll ans = ;
for (int i=; i<; ++i)
for (int j=; j<; ++j)
ans = (ans+dp[][str.size()-][i][j])%mod;
cout << ans << endl;
return ;
}

CodeForces 149D Coloring Brackets的更多相关文章

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

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

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

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

  3. CodeForces 149D Coloring Brackets 区间DP

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

  4. CodeForces 149D Coloring Brackets (区间DP)

    题意: 给一个合法的括号序列,仅含()这两种.现在要为每对括号中的其中一个括号上色,有两种可选:蓝or红.要求不能有两个同颜色的括号相邻,问有多少种染色的方法? 思路: 这题的模拟成分比较多吧?两种颜 ...

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

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

  6. codeforce 149D Coloring Brackets 区间DP

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

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

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

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

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

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

随机推荐

  1. Winform 数据库连接app.config文件配置 数据库连接字符串

    1.添加配置文件 新建一个winform应用程序,类似webfrom下有个web.config,winform下也有个App.config;不过 App.config不是自动生成的需要手动添加,鼠标右 ...

  2. 单线程vs多线程

    a.多线程可以说是实现异步的一种方式: b.共同点:多线程和异步操作两者都可以达到避免调用线程阻塞的目的,从而提高软件的可响应性: c.线程消费CPU资源.  异步消费硬件资源: 1.多线程和异步操作 ...

  3. 20+功能强大的jQuery/CSS3图片特效插件

    以下是分享的20几个不错的图片特效插件,基于jQuery和CSS3. 1.jQuery图片下滑切换播放效果 这是一款基于jQuery的焦点图插件,这款焦点图的特点是有向下滑动的动画效果,滑到底部时,有 ...

  4. Sql Server:不允许 ASSIGNMENT 语句中包含 FOR XML 子句

    编写函数的时候遇到“不允许 ASSIGNMENT 语句中包含 FOR XML 子句”错误,开始以为数据库函数里不可以写 FOR XML 子句,仔细看了看总觉得这么写别扭索性改了一种写法就通过了. BE ...

  5. Ubuntu中QT使用FFmpeg的奇怪问题

    FFmpeg都已经编译安装好了,QT的程序中调用av_register_all却总是在链接阶段报错,经过长时间的摸索,发现时静态链接库的问题,网上给出的答案都只能解决部分问题,所需的全部链接库如下: ...

  6. MyBatis知多少(21)更新操作

    上一章展示了如何使用MyBatis对表进行读取操作.本章将告诉你如何在一个表中使用MyBatis更新记录. 我们已经在MySQL下有EMPLOYEE表: CREATE TABLE EMPLOYEE ( ...

  7. android 中targetSdkVersion和与target属性的区别

    AndroidMenifest.xml中targetSdkVersion和project.properties中的target属性的区别      在AndroidMenifest.xml中,常常会有 ...

  8. Asp.Net(C#)自动执行计划任务的程序实例分析

    在业务复杂的应用程序中,有时候会要求一个或者多个任务在一定的时间或者一定的时间间隔内计划进行,比如定时备份或同步数据库,定时发送电子邮件等,我们称之为计划任务.实现计划任务的方法也有很多,可以采用SQ ...

  9. Supervisor – 用于 Unix 系统的进程监控工具

    Supervisor 是一个客户端/服务端模式的系统,使用户能够监视和控制 UNIX 操作系统的进程.Supervisor 为你提供一个地方来启动,停止和监视进程.进程可以单独或成组的形式控制.您还可 ...

  10. Android学习笔记之使用百度地图实现地图控制

    PS:吾之荣耀,离别已久. 学习内容: 1.实现地图控制. 2.百度地图开发的一些细节     1.实现地图控制:   这一篇主要写在百度地图上添加一些其他控制.上一篇书写了覆盖物的添加,地理编码和反 ...