codeforce 149 D. Coloring Brackets(区间dp)

D. 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 modulo 1000000007(109 + 7).

Examples
input
Copy
(())
output
Copy
12
input
Copy
(()())
output
Copy
40
input
Copy
()
output
Copy
4

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=705;
const ll mod=1e9+7;
ll dp[maxn][maxn][3][3];//dp[i][j][c1][c2]表示区间i~j第i个位置选c1颜色,
                        //第j个位置选c2颜色的方案数
int f[maxn];
char t[maxn];
void match()//预处理匹配信息,f[i]表示i与f[i]匹配
{
    stack<int>P;
    int L=strlen(t+1);
    for(int i=1;i<=L;i++)
    {
        if(t[i]=='(') P.push(i);
        else
        {
            f[P.top()]=i;
            P.pop();
        }
    }
}
bool check(int a,int b)//检查两个相邻括号
{
    return (a!=b)||(a==0&&b==0);
}
ll dfs(int L,int R,int c1,int c2)
{
    if(dp[L][R][c1][c2]!=-1)return dp[L][R][c1][c2];
    if(L+1>R) return dp[L][R][c1][c2]=0;
    //if(!((c1&&!c2)||(!c1&&c2)))return dp[L][R][c1][c2]=0;
    if(L+1==R)
    {
        if((c1&&!c2)||(!c1&&c2))dp[L][R][c1][c2]=1;
        else dp[L][R][c1][c2]=0;
        return dp[L][R][c1][c2];
    }
    int k=f[L];ll ans=0;
    if(k==R)
    {
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                if(check(c1,i)&&check(j,c2)&&((c1&&!c2)||(!c1&&c2)))
                {
                    ans=(ans+dfs(L+1,R-1,i,j))%mod;
                }
            }
        }
    }
    else
    {
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                if(check(i,j))
                {
                    ans=(ans+dfs(L,k,c1,i)*dfs(k+1,R,j,c2))%mod;
                }
            }
        }
    }
    return dp[L][R][c1][c2]=ans;
}
int main()
{
    memset(dp,-1,sizeof(dp));
    scanf("%s",t+1);
    match();
    ll ans=0;
    int L=strlen(t+1);
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            ans=(ans+dfs(1,L,i,j))%mod;
        }
    }
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/albertluf/article/details/80685513
今日推荐