CodeForces - 149D Coloring Brackets (区间DP+DFS)

探讨如何计算一种特定括号序列中,满足条件的染色方案数量。该问题要求将括号序列中的括号染成红或蓝,且满足配对括号颜色不同及相邻颜色不相同等条件。通过动态规划算法实现高效求解。

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.

思路:dp[l][i][r][j],i和j分别为第l个括号和第个括号的颜色,先匹配好括号,然后按括号来搜索,

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<math.h>
#include<stack>
using namespace std;
const long long mod=1000000007;
char str[1100];
long long mp[1100],dp[1100][3][1100][3];
stack<int>q;
int dfs(int l,int r)
{
    if(l+1==r)
    {
        dp[l][1][r][0]=dp[l][2][r][0]=dp[l][0][r][1]=dp[l][0][r][2]=1;
        return 0;
    }
    if(mp[l]==r)
    {
        dfs(l+1,r-1);
        for(int i=0; i<3; i++)
        {
            for(int j=0; j<3; j++)
            {
                if(i!=1) dp[l][0][r][1]=(dp[l][0][r][1]+dp[l+1][i][r-1][j])%mod;
                if(i!=2) dp[l][0][r][2]=(dp[l][0][r][2]+dp[l+1][i][r-1][j])%mod;
                if(j!=1) dp[l][1][r][0]=(dp[l][1][r][0]+dp[l+1][i][r-1][j])%mod;
                if(j!=2) dp[l][2][r][0]=(dp[l][2][r][0]+dp[l+1][i][r-1][j])%mod;
            }
        }
    }
    else
    {
        dfs(l,mp[l]);
        dfs(mp[l]+1,r);
        int u=mp[l];
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                for(int ki=0;ki<3;ki++)
                {
                    for(int kj=0;kj<3;kj++)
                    {
                        if(j&&j==ki) continue;
                        dp[l][i][r][kj]=(dp[l][i][r][kj]+dp[l][i][u][j]*dp[u+1][ki][r][kj])%mod;
                    }
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%s",str+1))
    {
        int len=strlen(str+1);
        for(int i=1; i<=len; i++)
        {
            if(str[i]=='(')
                q.push(i);
            else
            {
                mp[q.top()]=i;
                q.pop();
            }
        }
        memset(dp,0,sizeof(dp));
        dfs(1,len);
        long long ans=0;
        for(int i=0;i<3;i++)
            for(int j=0;j<3;j++)
            ans+=dp[1][i][len][j];
        printf("%I64d\n",ans%mod);
    }
}

 

引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.youkuaiyun.com/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值