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.

                                  

题目大意:

给定一个合法的括号序列,每对括号有且只能涂一种颜色(一半红色或蓝色,一半不涂),且相邻的两个位置不能涂同一种颜色,求有多少种涂法。

dp[i][j][x][y]:i,j分别代表左右段点,x,y分别代表左右端点的颜色。

若当前的i,j是一对括号dp[i][j][][]则由dp[i+1][j-1][][]推来,反之,找到与i匹配的mid,dp[i][j][][]则由dp[i][mid][][]与dp[mid+1][j][][]推来。

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int MOD=1e9+;
ll dp[][][][];///左端点,右端点,左颜色,右颜色
int has[];
stack<int> st;
void dfs(int l,int r)
{
if(l==r) return;
if(l+==r)
{
dp[l][r][][]=;
dp[l][r][][]=;
dp[l][r][][]=;
dp[l][r][][]=;
return;
}
if(has[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(i!=) 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;
}
}
return;
}
int mid=has[l];
dfs(l,mid);
dfs(mid+,r);
for(int i=;i<;i++)
for(int j=;j<;j++)
{
for(int k=;k<;k++)
for(int m=;m<;m++)
{
if(k==m&&k) continue;
dp[l][r][i][j]+=(dp[l][mid][i][k]*dp[mid+][r][m][j])%MOD;
}
dp[l][r][i][j]%=MOD;
}
return;
}
int main()
{
ios::sync_with_stdio(false);
string s;
cin>>s;
for(int i=;s[i];i++)///括号匹配
{
if(s[i]=='(')
st.push(i);
else
{
has[st.top()]=i;
has[i]=st.top();
st.pop();
}
}
dfs(,s.size()-);
ll ans=;
for(int i=;i<;i++)
for(int j=;j<;j++)
ans+=dp[][s.size()-][i][j];
cout<<ans%MOD<<'\n';
return ;
}

Coloring Brackets (区间DP)的更多相关文章

  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 Round #106 (Div. 2) D. Coloring Brackets —— 区间DP

    题目链接:https://vjudge.net/problem/CodeForces-149D D. Coloring Brackets time limit per test 2 seconds m ...

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

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

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

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

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

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

  6. Codeforces149D - Coloring Brackets(区间DP)

    题目大意 要求你对一个合法的括号序列进行染色,并且需要满足以下条件 1.要么不染色,要么染红色或者蓝色 2.对于任何一对括号,他们当中有且仅有一个被染色 3.相邻的括号不能染相同的颜色 题解 用区间d ...

  7. codeforce 149D Coloring Brackets 区间DP

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

  8. CodeForces 149D Coloring Brackets 区间DP

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

  9. Codeforces 508E Arthur and Brackets 区间dp

    Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...

  10. POJ 2995 Brackets 区间DP

    POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...

随机推荐

  1. PWA之push服务

    转载: https://www.jishux.com/p/c5735af96c39bd4a https://www.jianshu.com/p/9970a9340a2d 系列文章参考:https:// ...

  2. 4. iOS测试常用方法

    1.    [XCUIElement exists]方法只能确定这个View是否存在,即使不在当前屏幕上也返回True.如果要确定View是否在屏幕可见范围内,可以判断其Frame是否在Window的 ...

  3. BZOJ3083: 遥远的国度(树链剖分)

    题意 $n$个节点的树,每个点有权值,支持三种操作 1. 换根 2.把$x$到$y$路径上节点权值变为$z$ 3.询问路径最小值 Sol 啥?你说这是TopTree的裸题?那你写去啊 很显然,如果没有 ...

  4. Mac OSX简单使用中会用到的

    选择操作系统(例如选择BootCamp分区的Windows):开机按住Option键直到磁盘图标出现后选择. 忘记本地账号密码:按着Command+R开机选择Recovered启动打开终端输入re ...

  5. powerDesigner的name和comment转化

    name2comment.vbs '****************************************************************************** '* ...

  6. diff - 找出两个文件的不同点

    总览 diff [选项] 源文件 目标文件 描述 在最简单的情况是, diff 比较两个文件的内容 (源文件 和 目标文件). 文件名可以是 - 由标准输入设备读入的文本. 作为特别的情况是, dif ...

  7. vueCode 常用代码总结 20190116

    <template>props 传参<in-body :mbx="['首页','','']"> props 代码使用<BreadcrumbItem&g ...

  8. QT_3

    1.QT中命名的规范和常用的快捷键 1.1 命名规范: 类名:首字母大写    多个单词时单词与单词之间首 字母大写 函数名:变量名称   首字母小写    多个单词时,单词和单词之间首字母大写 1. ...

  9. idea创建Maven项目时Maven插件内看不到mybatis-generator

    创建Maven项目时插件配置添加了mybatis-generator但是右侧maven project始终没有看到插件 需要放在和pluginManagement同级别,修改配置如下:

  10. django 127.0.0.1 将您重定向的次数过多

    "GET /?next=/%3Fnext%3D/%253Fnext%253D/ HTTP/1.1" 302 0 solution reference from django.con ...