CodeForces 149D Coloring Brackets(区间dp+记忆化搜索)

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 modulo1000000007 (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,设置长度以及当前位置颜色和为匹配括号数为参数,但是并不能确定之前的未匹配括号的染色状态,无法保证一对括号有且仅有一个染了色,故抛弃这种想法。转而看到了有人说是区间dp,于是也开始考虑,但是这个区间dp不像是普通的区间dp,因为普通dp我是可以任意枚举中间节点进行合并,但是这里显然收到括号匹配关系的限制,因为如果说一个区间内的括号不合法,那么计算出的结果在区间之间就没有直接相加性。

        但是,经过思考还是可以发现正确的转移方式。首先,如果当前枚举的区间[l,r]两端点恰好是相互匹配的括号,那么显然可以从区间[l+1,r-1]转移过来。然后如果不匹配,因为我们每次在寻找的时候都要保证每一个小段括号匹配是要合法的,所以我们就只能拆成[l,mp[l]]和[mp[l],r]两个小区间,其中mp[l]表示括号l匹配的另一个括号的位置。这既能保持括号匹配合法的性质,区间也满足合并的条件。此时,方案数就是从这两个小区间方案数的乘积转移过来的。

        具体来说,我们设dp[l][r][i][j]表示区间[l,r]左端点颜色为i,右端点颜色为j时的方案数。然后有转移方程:dp[l][r][i][j]=Σdp[l+1][r-1][x][y](满足i、j中一个为0,且对应小区间染色括号颜色与大区间染色括号颜色不同);和dp[l][r][i][j]=Σdp[l][mp[l]][i][x]*dp[mp[l]+1][r][y][j](同样也是要满足颜色相同的限制)。可以看到,要知道大区间的值,必须知道小区间的值,但是又不能直接枚举先处理所有小区间,所以我们想到了利用记忆化搜索,每次转移之前先调用搜索处理小区间。如此处理即可解决此问题。具体见代码:

#include <bits/stdc++.h>
#define mod 1000000007
#define LL long long
#define N 800
using namespace std;

LL dp[N][N][3][3];
stack<int> sta;
int mp[N],n;
char s[N];

void dfs(int l,int r)
{
    if (r-l==1)
    {
        dp[l][r][1][0]=1;
        dp[l][r][2][0]=1;
        dp[l][r][0][1]=1;
        dp[l][r][0][2]=1;
        return;
    }
    if (r==mp[l])								//括号匹配,那么直接从[l+1,r-1]转移
    {
        dfs(l+1,r-1);
        for(int i=0;i<=2;i++)
            for(int j=0;j<=2;j++)
            {
                if (j!=1)						//相邻括号颜色不能相同
                    dp[l][r][0][1]=(dp[l][r][0][1]+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;
                if (i!=1)
                    dp[l][r][1][0]=(dp[l][r][1][0]+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;
            }
    } else
    {
        dfs(l,mp[l]);
        dfs(mp[l]+1,r);
        for(int i=0;i<=2;i++)
            for(int j=0;j<=2;j++)
                for(int c1=0;c1<=2;c1++)
                    for(int c2=0;c2<=2;c2++)
                    {
                        if (c1==c2&&c1) continue;			//如果颜色相同且涂了颜色,那么此状态不合法
                        dp[l][r][i][j]=(dp[l][r][i][j]+dp[l][mp[l]][i][c1]*dp[mp[l]+1][r][c2][j])%mod;
                    }
    }
}

int main()
{
    scanf("%s",s+1);
    n=strlen(s+1);
    for(int i=1;i<=n;i++)
    {
        if (s[i]=='(') sta.push(i);
        if (s[i]==')')
        {
            int x=sta.top();
            sta.pop(); mp[x]=i; mp[i]=x;
        }
    }
    dfs(1,n); LL ans=0;
    for(int i=0;i<=2;i++)
        for(int j=0;j<=2;j++)
            ans=(ans+dp[1][n][i][j])%mod;
    printf("%I64d\n",ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值