Codeforces Round #106 (Div. 2) D. Coloring Brackets

24 篇文章 0 订阅
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
(())
output
12
input
(()())
output
40
input
()
output
4
题意是给一串已经匹配好的括号,然后对括号进行染色,有以下要求:
1、每个括号要么不染色要么染红色要么染蓝色;
2、相邻的括号不能染同样的颜色,但是可以都不染色;
3、每对匹配好的括号只能且必须有一个染色;
问总共有多少次匹配方法。
 
dp题,大概要记三个状态,每个括号的颜色,和匹配的括号的状态还有相邻状态。一开始想了一个两维DP,dp[i][k][j][l]代表第i个括号染成k颜色且第j个括号染成l颜色的情况。但是想了一下找不到递推关系。再考虑一下区间的做法,dp[i][j][k][l]代表从i到j且左端点染成颜色k,右端点染成颜色l的情况,(左右断电染色情况可以自己定义,我定义1为无色,2为红色,3为蓝色 )。那么我们就可以转移了
如果第j个括号的匹配括号不在i到j之间,那么就不用考虑匹配括号的情况直接考虑相邻的情况,具体的转移方程推起来也不麻烦,直接按照题意去写就OK。
但是如果第j个括号的匹配在i到j之间的话,假设br[j]是第j个括号的匹配,那么dp[i][j] 应该等于dp[i][ br[j] - 1 ] 和 dp[ br[j] ][j]这两段去乘来乘去,具体的转移过程也比较好推按照题意来就行。这样做的好处就是简化了处理括号匹配的问题。
这个类型区间DP没怎么做过,之前做的是枚举断点的区间DP,其实这种也是一种很巧妙的转移方法,学习一下。
由史上最难看的dp代码
 
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const LONG   INF=0x3f3f3f3f;
const LONG    MOD=1e9+7;
const double PI=acos(-1.0);
#define clr0(x) memset(x,0,sizeof x)
#define clrI(x) memset(x,-1,sizeof(x))
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
long long  dp[800][800][5][5];
int br[800] ;
char str[800];
int main()
{
    clr0(dp);
    clr0(br);
    scanf("%s",str + 1) ;
    str[0] = '&' ;
    int n = strlen(str) - 1;
    for(int i = 1; i<= n ; ++ i)
        for(int j = 1; j<= 3 ;++ j)
                dp[i][i][j][j] = 1;
    for(int i = 1; i<= n ;++ i)
    {
        if(str[i] ==')')
        {
            int sum = 1;
            for(int j = i - 1; j >= 1; -- j)
                if(str[j] == '(')sum --;
                else sum ++ ;
                if(!sum)
                {
                    br[i] = j;
                    br[j] = i;
                    break ;
                }
        }
    }
    for(int l = 1; l < n ; ++l)
    {
        for(int i = 1; i < n ;++ i)
        {
            int j = i + l ;
            if(j>n)break ;
            if(br[j] > i && br[j] < j - 1)
            {
                dp[i][j][1][1] += dp[i][br[j] - 1][1][1] * (dp[br[j]][j][2][1]  + dp[ br[j] ][j][3][1]) ;
                dp[i][j][1][1] += dp[i][br[j] - 1][1][3] * dp[br[j]][j][2][1] ;
                dp[i][j][1][1] += dp[i][br[j]-1][1][2] * dp[ br[j] ][j][3][1] ;
                dp[i][j][1][2] += dp[i][br[j] - 1][1][1] * dp[ br[j] ][j][1][2] ;
                dp[i][j][1][2] += dp[i][br[j]- 1][1][2] * dp[br[j] ][j][1][2] ;
                dp[i][j][1][2] += dp[i][br[j] - 1][ 1 ][3] * dp[br[j]][j][1][2] ;
                dp[i][j][1][3] += dp[i][br[j] - 1][1][1] * dp[ br[j] ][j][1][3] ;
                dp[i][j][1][3] += dp[i][br[j]- 1][1][2] * dp[br[j] ][j][1][3] ;
                dp[i][j][1][3] += dp[i][br[j] - 1][ 1 ][3] * dp[br[j]][j][1][3] ;
                dp[i][j][2][1] += (dp[i][br[j] - 1][2][1] + dp[i][br[j] - 1][2][3]) * dp[ br[j] ][j][2][1] ;
                dp[i][j][2][1] += ( dp[i][br[j]- 1][2][2] + dp[i][br[j] - 1][2][1] ) * dp[br[j] ][j][3][1] ;
                dp[i][j][2][2] += (dp[i][br[j] - 1][2][1] + dp[i][br[j] - 1][2][3] + dp[i][br[j] - 1][2][2])  * dp[ br[j] ][j][1][2] ;
                dp[i][j][2][3] += (dp[i][br[j] - 1][2][1] + dp[i][br[j] - 1][2][3] + dp[i][br[j] - 1][2][2])  * dp[ br[j] ][j][1][3] ;
                dp[i][j][3][1] += (dp[i][br[j] - 1][3][1] + dp[i][br[j] - 1][3][3]) * dp[ br[j] ][j][2][1] ;
                dp[i][j][3][1] += ( dp[i][br[j]- 1][3][2] + dp[i][br[j] - 1][3][1] ) * dp[br[j] ][j][3][1] ;
                dp[i][j][3][2] += (dp[i][br[j] - 1][3][1] + dp[i][br[j] - 1][3][3] + dp[i][br[j] - 1][3][2])  * dp[ br[j] ][j][1][2] ;
                dp[i][j][3][3] += (dp[i][br[j] - 1][3][1] + dp[i][br[j] - 1][3][3] + dp[i][br[j] - 1][3][2])  * dp[ br[j] ][j][1][3] ;
                for(int k =1 ;k<=3 ;++k)for(int t = 1; t <= 3; ++ t)dp[i][j][k][t] %= MOD;
                continue ;
            }
            if(br[j] != i && br[j] != j - 1)
            {
                for(int k = 1; k <= 3 ;++ k)
                {
                    dp[i][j][k][1] += (dp[i][j - 1][k][1] + dp[i][j - 1][k][2] +dp[i][j -1 ][k][3] )  ;
                    dp[i][j][k][1] %= MOD;
                    dp[i][j][k][2] += dp[i][j-1][k][1] + dp[i][j-1][k][3];
                    dp[i][j][k][2] %= MOD;
                    dp[i][j][k][3] += dp[i][j-1][k][2] + dp[i][j-1][k][1] ;
                    dp[i][j][k][3] %= MOD;
                }
            }
            else if(br[j] == j - 1 && i == j - 1)
            {

                dp[i][j][1][2] += dp[i][j-1][1][1] ;
                dp[i][j][1][3] += dp[i][j-1][1][1] ;
                dp[i][j][2][1] += dp[i][j-1][2][2] ;
                dp[i][j][3][1] += dp[i][j-1][3][3] ;
            }
            else if(br[j] == j-1 && i != j - 1)
            {
                for(int k =1; k<= 3 ;++ k)
                {
                    dp[i][j][k][1] += dp[i][j-1][k][2] + dp[i][j-1][k][3] ;
                    dp[i][j][k][1] %= MOD;
                    dp[i][j][k][2] += dp[i][j-1][k][1] ;
                    dp[i][j][k][2] %= MOD;
                    dp[i][j][k][3] += dp[i][j-1][k][1] ;
                    dp[i][j][k][3] %= MOD;
                }
            }
            else if(br[j] == i)
            {
                dp[i][j][2][1] += dp[i][j-1][2][1] + dp[i][j-1][2][2] + dp[i][j-1][2][3] ;
                dp[i][j][2][1] %= MOD;
                dp[i][j][3][1] += dp[i][j-1][3][1] + dp[i][j-1][3][2] + dp[i][j-1][3][3] ;
                dp[i][j][3][1] %= MOD;
                dp[i][j][1][2] += dp[i][j-1][1][1] + dp[i][j-1][1][3] ;
                dp[i][j][1][2] %= MOD;
                dp[i][j][1][3] += dp[i][j-1][1][1] + dp[i][j-1][1][2] ;
                dp[i][j][1][3] %= MOD;
            }
        }
    }
    LONG ans = 0;
    for(int i = 1; i<=3 ; ++ i)
        for(int j = 1 ; j<= 3 ;++ j)
            ans += dp[1][n][i][j] ,
            ans %= MOD;
    cout<<ans<<endl;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值