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

Description

点击查看题面

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.

输入描述:

The first line contains the single string s (2 ≤ |s| ≤ 700) which represents a correct bracket sequence.

输出描述:

Print the only number — the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007

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

Probelm solving

这道题的意思就是给你一个合格的括号序列,每一组括号(左括号和右括号)必须且只能选出来一个进行染色,有两种颜色可以选择,并且相邻的字符所染的颜色不能相同,如果都没有染色那是可以的。现在问你这个序列有多少种合法的染色方式。
比如或括号序列为,那么就有四种染色方式,我们把两种颜色记为,
这四种染色方式就是,,,

既然是区间dp的专题那一定得忘dp那边想啊。看了好多题解之后才知道这道题怎么写。
首先我们开一个思维的数组进行dp,,代表的含义就是位置上染的色为位置上染的色为

所代表的就是从这个区间内的括号的染色方案数。
因为限制条件里面还有一条是说,同一组括号只能并且必须有一个进行染色,所以我们首先得记录一下每个括号与之对应的另外一个括号,用一个栈就可以解决了。
我们从这个区间开始进行dfs,采用记忆化搜索,如果现在的区间长度为了,它的取值就是固定的了,这就是dfs的结束条件.
如果区间长度大于一
并且当此时的区间两端是对应的括号的时候,我们需要继续向之前的区间搜索(即
转移方程为

dp[l][r][i][j]=(dp[l][r][i][j]+dp[l+1][r-1][k][p])%mod

因为它可以有上一个区间的值得到现在的值,我们通过对于染色的限定条件进行判断,满足的话就更新

如果区间两端的括号不相对应,我们就把这个区间拆分为两个区间接着往前搜索(即,指的是与l位上的括号对应的括号的位置

转移方程为

dp[l][r][i][j]=(dp[l][r][i][j]+dp[l][m[l]][i][k]*dp[m[l]+1][r][p][j]%mod)%mod

这就是区间dp比较明显的一个地方,如果他不是对应的括号,那我们就在这个区间内找到与他对应的,然后因为两个区间没有任何影响,所以我们应该取乘积。

关于染色条件的限制,因为我们dp数组的后两维表示的就是颜色,我们只需要判断后两维即可。

Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
const ll maxn = 1e3+10;
const ll mod = 1e9+7;
const ll INF = 0x3f3f3f3f;
ll d[4][2]={1,0,0,1,-1,0,0,-1};
ll a[maxn],m[maxn],vis[maxn][maxn],dp[maxn][maxn][3][3];
void dfs(ll l,ll r){
    if(vis[l][r])   return ;
    vis[l][r]=1; //记忆化搜索
    if(l+1==r){
        dp[l][r][0][1]=dp[l][r][1][0]=1;
        dp[l][r][0][2]=dp[l][r][2][0]=1;
    }else if(m[l]==r){
        dfs(l+1,r-1);
        for(ll i=0;i<3;i++){
            for(ll j=0;j<3;j++){
                for(ll k=0;k<3;k++){
                    for(ll p=0;p<3;p++){
                        if((i==0||j==0)&&(i!=0||j!=0)&&(i!=k||(i==0||k==0))&&(j!=p||(j==0||p==0)))//i和j是匹配位置上的两个染色需要进行判断,i和k,j和p是两组相邻的颜色所以也需要进行判断
                            dp[l][r][i][j]=(dp[l][r][i][j]+dp[l+1][r-1][k][p])%mod;
                    }
                }
            }
        }
    }else{
        dfs(l,m[l]);dfs(m[l]+1,r);
        for(ll i=0;i<3;i++){
            for(ll j=0;j<3;j++){
                for(ll k=0;k<3;k++){
                    for(ll p=0;p<3;p++){
                        if((i==0||k==0)&&(i!=0||k!=0)&&(k!=p||(k==0&&p==0)))//因为i和k是两个匹配位置上染的色,所以需要进行判断,k和p是相邻的两个位置所以需要判断
                            dp[l][r][i][j]=(dp[l][r][i][j]+dp[l][m[l]][i][k]*dp[m[l]+1][r][p][j]%mod)%mod;
                    }
                }
            }
        }
    }
}
int main() {
    ios::sync_with_stdio(0);
    #ifdef Uncle_drew
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #else
    #endif
    string s,t=" ";
    cin>>s;t+=s;
    ll len=t.size()-1,pos=1;
    for(int i=1;i<=len;i++){
        if(t[i]=='(')   a[pos++]=i;
        else m[a[--pos]]=i;
    }
    dfs(1,len);
    ll ans=0;
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            ans=(ans+dp[1][len][i][j])%mod;
        }
    }//所有答案累积起来
    cout<<ans<<endl;
    return 0;
}