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. PopupWindow(3)back,home 键无法关闭popupwindow的解决方案

    private PopupWindow mPopupWindow; //popup window 一般popuowindow 要都个显示view,本例子中view模拟菜单. private View ...

  2. C#基础学习4

    流程控制!

  3. asp.net core教程 (一)

    Asp.Net Core简介 ASP.NET Core 是一个全新的开源.跨平台框架,可以用它来构建基于网络连接的现代云应用程序,比如:Web 应用,IoT(Internet Of Things,物联 ...

  4. Ubuntu16.04常用操作命令总结ing

    查看软件安装目录:whereis 软件名称(如:whereis mysql,where is sqlite3等) 安装软件:apt/apt-get install 软件名称(如:apt/apt-get ...

  5. find、filter、map的区别

    1.find 查询数组中符合条件的第一个元素,如果没有符合条件的元素则返回空数组 ,,,,,,] ) var men=[ {name:',sex:'女'}, {name:',sex:'nan'}, { ...

  6. 油猴 tamperMonkey 在百度首页 添加自己的自定义链接

    发现 GM_addStyle 函数不能用了,从写加载css函数. 剩下找个定位 添加内容 就很简单了. // ==UserScript== // @name helloWorld // @namesp ...

  7. PHP16 PHP访问MySQL

    学习要点 PHP访问MySQL配置 PHP访问MySQL函数介绍 足球赛程信息管理 PHP访问MySQL配置 PHP.ini配置文件确认以下配置已经打开 extension=php_mysql.dll ...

  8. 日常[splay]:水题记——普通平衡树(死亡调试)

    普通平衡树,模板的不能再模板的模板题.我调了两个小时... 早先看yyb大神的blog学习splay,看的风生水起然而没有发现,大神的坑没有填……没有rank操作和k_th操作. 只能自己摸索,问问大 ...

  9. Vue之组件的使用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. JAVA用freemarker生成复杂Excel。(freemarker)

    在生成Excel的时候,大多时候都是使用poi,jxl等进行的,但是对于复杂的Excel来说,这个工作量是非常的大的,而且,对于我这么懒的人来说,这是相当痛苦的一件事情,所以,我不得不找找有没有简单一 ...