动态规划——Coloring Brackets

来源:互联网 发布:win8.1一键优化工具 编辑:程序博客网 时间:2024/06/06 07:02

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

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

 

题意:给一个括号序列,让你涂色共有多少种方案,不要求每个括号都涂色。

涂色规则:

1.颜色有红和蓝。

2.每对括号只涂其中一个

3.相邻的括号不能涂相同的颜色

思路:

先预处理出每个左括号与之匹配的右括号的下标和每个右括号与之匹配的左括号下标。

令0代表不涂,1代表红,2代表蓝。

可以设置数组dp[l][r][i][j]代表l涂i颜色,r涂j颜色时的方案数。

则对于任意l和r来说有两种情况:

1.如果l和r是匹配的,那么dfs搜索(l+1,r-1)

2.如果l和r不匹配,求出与l匹配的下标pos,dfs搜索(l,pos)和(pos+1, r)

详细见代码


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define mod 1000000007using namespace std;typedef long long ll;char a[750];int n;int index[750];         //储存匹配的下标int b[750];ll dp[750][750][3][3];void dfs(int l, int r){    if(l+1==r)             //返回的条件    {        dp[l][r][0][1]=1;        dp[l][r][1][0]=1;        dp[l][r][2][0]=1;        dp[l][r][0][2]=1;        return;    }    if(index[l]==r)    {        dfs(l+1, r-1);        for(int i=0; i<3; i++)            for(int j=0; j<3; j++)            {                if(i!=1)                    dp[l][r][1][0]=(dp[l][r][1][0]+dp[l+1][r-1][i][j])%mod;                if(j!=1)                    dp[l][r][0][1]=(dp[l][r][0][1]+dp[l+1][r-1][i][j])%mod;                if(i!=2)                    dp[l][r][2][0]=(dp[l][r][2][0]+dp[l+1][r-1][i][j])%mod;                if(j!=2)                    dp[l][r][0][2]=(dp[l][r][0][2]+dp[l+1][r-1][i][j])%mod;            }    }    else    {        int pos=index[l];        dfs(l,pos);        dfs(pos+1, r);        for(int i=0; i<3; i++)            for(int j=0; j<3; j++)                for(int x=0; x<3; x++)                    for(int y=0; y<3; y++)                        if((x!=y)||(x==0&&y==0))      //相邻的颜色不能相同                            dp[l][r][i][j]=(dp[l][r][i][j]+(dp[l][pos][i][x]*dp[pos+1][r][y][j])%mod)%mod;    }}int main(){    while(~scanf("%s", a+1))    {        n=strlen(a+1);        int k=0;        memset(index, 0, sizeof(index));        memset(dp, 0, sizeof(dp));        for(int i=1; i<=n; i++)       //预处理            if(a[i]=='(')                b[k++]=i;            else            {                 index[i]=b[k-1];                 index[b[k-1]]=i;                 k--;            }        dfs(1,n);        ll ans=0;        for(int i=0; i<3; i++)            for(int j=0; j<3; j++)                ans=(ans+dp[1][n][i][j])%mod;             //求每种涂色方案的累加和        printf("%lld\n", ans);    }    return 0;}


原创粉丝点击