Codeforces 149D Coloring Brackets【区间Dp+思维】好题!好题!

351 篇文章 2 订阅

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
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、首先我们栈匹配出数组match【i】=x,表示左括号i匹配的右括号的位子在x.


2、然后观察到数据范围。又是要取模统计方案数的问题。

那么考虑区间dp,设定dp【i】【j】【3(k)】【3(l)】表示对于区间【i,j】染色,位子i染色为k,位子j染色为l的方案数。


问题给出的字符串保证是整个匹配的字符串,那么我们先处理小的完整匹配的区间,再来得到大的完整匹配的区间。

那么我们只要将此时的(i,j)分成两类去讨论即可:


①match【i】==j.那么对于此时区间【i,j】是一个完整的匹配串,那么肯定也有【i+1,j-1】也是一个完整的匹配串,那么其状态转移方程只要注意染色规则即可:



②match【i】!=j.那么对于这个区间【i,j】就不是一个完整匹配的区间.那么我们已知match【i】,那么我们可以将区间【i,j】分成两个子区间:【i,match【i】】【match【i】+1,j】;

同时注意一下括号染色规则去转移即可:




注意取模和初始化以及代码实现细节。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
#define ll __int64
char a[800];
int match[800];
ll dp[800][800][3][3];
ll mod=1e9+7;
int main()
{
    while(~scanf("%s",a+1))
    {
        stack<int>s;
        int n=strlen(a+1);
        for(int i=1;i<=n;i++)
        {
            if(a[i]=='(')s.push(i);
            else
            {
                match[s.top()]=i;
                s.pop();
            }
        }
        memset(dp,0,sizeof(dp));
        for(int len=1;len<=n;len++)
        {
            for(int i=1;i<=n;i++)
            {
                int j=len+i;
                if(j>n)break;
                if(len==1)
                {
                    if(match[i]==j)
                    {
                        dp[i][j][0][1]=1;
                        dp[i][j][1][0]=1;
                        dp[i][j][0][2]=1;
                        dp[i][j][2][0]=1;
                    }
                }
                else
                {
                    if(match[i]==j)
                    {
                        for(int l=0;l<3;l++)
                        {
                            for(int r=0;r<3;r++)
                            {
                                for(int l2=0;l2<3;l2++)
                                {
                                    for(int r2=0;r2<3;r2++)
                                    {
                                        if(l==l2&&l>0)continue;
                                        if(r==r2&&r>0)continue;
                                        if(l==0&&r==0)continue;
                                        if(l==0||r==0)
                                        {
                                            dp[i][j][l][r]+=dp[i+1][j-1][l2][r2];
                                            dp[i][j][l][r]%=mod;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        int k=match[i];
                        for(int l=0;l<3;l++)
                        {
                            for(int r=0;r<3;r++)
                            {
                                for(int l2=0;l2<3;l2++)
                                {
                                    for(int r2=0;r2<3;r2++)
                                    {
                                        if(r==l2&&r>0)continue;
                                        dp[i][j][l][r2]+=((dp[i][k][l][r]%mod)*(dp[k+1][j][l2][r2])%mod)%mod;
                                        dp[i][j][l][r2]%=mod;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        int output=0;
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                output+=dp[1][n][i][j];
                output%=mod;
            }
        }
        output%=mod;
        printf("%d\n",output);
    }
}




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值