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. luogu P3371 & P4779 单源最短路径spfa & 最大堆优化Dijkstra算法

    P3371 [模板]单源最短路径(弱化版) 题目背景 本题测试数据为随机数据,在考试中可能会出现构造数据让SPFA不通过,如有需要请移步 P4779. 题目描述 如题,给出一个有向图,请输出从某一点出 ...

  2. 把AM_B_ENTRY表里的ARCHIVAL_CODE字段,值复制给BA_ARCHIVAL_CODE_160812字段

    UPDATE AM_B_ENTRY T SET T.BA_ARCHIVAL_CODE_160812=T.ARCHIVAL_CODE SELECT BA_ARCHIVAL_CODE_160812 FRO ...

  3. UVa OJ 494

     Kindergarten Counting Game  Everybody sit down in a circle. Ok. Listen to me carefully. ``Woooooo, ...

  4. day02 -操作系统及python入门

    操作系统 1.什么是操作系统? 操作系统位于计算机硬件和应用软件之间. 是一个协调.控制.管理计算机硬件资源和软件资源的控制程序. 2.为何要有操作系统? ①·控制硬件 ②·把对硬件的复杂的操作封装成 ...

  5. 开发原生安卓cordova插件(有原生界面)

    上文开发的插件没有调用原生界面,本文介绍开发带有activity的插件 本文很多操作与上文重复,重复部分会省略 首先打开plug1,先开发插件的原生代码 在以下命名空间创建一个activity 名称为 ...

  6. IOS 根据身份证号码获取 年龄 生日 性别

    /** 从身份证上获取年龄 18位身份证 */ -(NSString *)getIdentityCardAge:(NSString *)numberStr { NSDateFormatter *for ...

  7. Linux系统使用iftop查看带宽占用情况

    Linux系统下如果服务器带宽跑满了,查看跟哪个ip通信占用带宽比较多,可以通过iftop命令进行查询,使用方法如下: 1 安装方法[软件官网地址:http://www.ex-parrot.com/~ ...

  8. CSS3实现单行、多行文本溢出(省略号的形式出现)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. [Github筆記] 清除所有 Commit 紀錄

    # 把原來的 git 移除掉 sudo rm .git -r # 初始化 git init git remote add origin https://github.com/username/repo ...

  10. [Python學習筆記] 抓出msg信件檔裡的附件檔案

    想要把msg信件檔案的附件抓出來做處理,找到了這個Python 模組 msg-extractor 使用十分容易,但是這個模組是要在terminal裡執行,無法直接打在IDLE的編輯器上 所以稍微做了修 ...