Codeforces 149D 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 modulo1000000007 (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).

Sample test(s)
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[i][j][c1][c2]表示 i染c1且j染c2时 区间[i, j]的染色方案。预处理括号i的匹配括号fp[i]。

考虑区间[i, j](c1, c2)

有两种情况:

一、i j是正好匹配的,判断是否合法后,统计符合条件的所有情况即可。

二、i j不匹配,那么将区间[i, j]分为[i, fp[i]]*[fp[i]+1, j],枚举合法的状态统计即可。


AC代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (100000+10)
#define MAXM (200000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#define first fi
#define second se
using namespace std;
char s[800];
LL dp[800][800][3][3];
int fp[MAXN];
bool judge(int c1, int c2){
    return c1 == 0 || c2 == 0 || c1 != c2;
}
LL DFS(int i, int j, int c1, int c2)
{
    if(dp[i][j][c1][c2] != -1) return dp[i][j][c1][c2];
    if(i + 1 > j) return dp[i][j][c1][c2] = 0;
    if(i == j) return dp[i][j][c1][c2] = 1LL;
    LL ans = 0; int k = fp[i];
    if(k == j)
    {
        if((c1 == 0 && c2) || (c1 && c2 == 0))
        {
            if(i + 1 == j) return dp[i][j][c1][c2] = 1LL;
            for(int a = 0; a <= 2; a++)
                for(int b = 0; b <= 2; b++)
                    if(judge(c1, a) && judge(b, c2))
                        ans = (ans + DFS(i+1, j-1, a, b)) % MOD;
        }
    }
    else if(k < j)
    {
        for(int a = 0; a <= 2; a++)
            for(int b = 0; b <= 2; b++)
                if(judge(a, b))
                    ans = (ans + DFS(i, k, c1, a) * DFS(k+1, j, b, c2)) % MOD;
    }
    return dp[i][j][c1][c2] = ans;
}
int main()
{
    Rs(s); int len = strlen(s);
    stack<int> S;
    for(int i = 0; i < len; i++)
    {
        if(s[i] == '(')
            S.push(i);
        else
        {
            fp[S.top()] = i;
            S.pop();
        }
    }
    CLR(dp, -1); LL ans = 0;
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
            ans = (ans + DFS(0, len-1, i, j)) % MOD;
    Pl(ans);
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值