CF149D Coloring Brackets

CF149D Coloring Brackets

题目链接

​ 区间DP。

​ 用\(f[l][r][0/1/2][0/1/2]\)表示在区间\([l, r]\)中,\(l\)位置的括号不染色/染红色/染蓝色,\(r\)位置的括号不染色/染红色/染蓝色的方案数。

​ 考虑转移:

​ 当\(l + 1 == r\)的时候,表示边界

f[l][r][0][1] = f[l][r][0][2] = f[l][r][1][0] = f[l][r][2][0] = 1;

​ 当\(l\)\(r\)括号匹配的时候,

dfs(l + 1, r - 1);
for(int i = 0;i <= 2; i++) 
            for(int j = 0;j <= 2; j++) {
                if(i != 1) (f[l][r][1][0] += f[l + 1][r - 1][i][j]) %= mod;
                if(i != 2) (f[l][r][2][0] += f[l + 1][r - 1][i][j]) %= mod;
                if(j != 1) (f[l][r][0][1] += f[l + 1][r - 1][i][j]) %= mod;
                if(j != 2) (f[l][r][0][2] += f[l + 1][r - 1][i][j]) %= mod;
            }

​ 当\(l\)\(r\)括号不匹配的时候,

dfs(l, match[l]); dfs(match[l] + 1, r); //match[l]表示与l匹配的右括号位置
        for(int i = 0;i <= 2; i++) 
            for(int j = 0;j <= 2; j++) 
                for(int p = 0;p <= 2; p++) 
                    for(int q = 0;q <= 2; q++) {
                        if((j == 1 && p == 1) || (j == 2 && p == 2)) continue;
                        (f[l][r][i][q] += 1ll * f[l][match[l]][i][j] * f[match[l] + 1][r][p][q] % mod) %= mod;
                    }

​ 完整代码:

#include <bits/stdc++.h>
    
using namespace std;
    
inline long long read() {
    long long s = 0, f = 1; char ch;
    while(!isdigit(ch = getchar())) (ch == '-') && (f = -f);
    for(s = ch ^ 48;isdigit(ch = getchar()); s = (s << 1) + (s << 3) + (ch ^ 48));
    return s * f;
}
    
const int N = 705;
const long long mod = 1000000007;
char ch[N];
int len, top, sta[N], match[N];
long long ans, f[N][N][3][3];

void dfs(int l, int r) {
    if(l + 1 == r) {
        f[l][r][0][1] = f[l][r][0][2] = f[l][r][1][0] = f[l][r][2][0] = 1;
        return ;
    }
    else if(match[l] == r) {
        dfs(l + 1, r - 1);
        for(int i = 0;i <= 2; i++) 
            for(int j = 0;j <= 2; j++) {
                if(i != 1) (f[l][r][1][0] += f[l + 1][r - 1][i][j]) %= mod;
                if(i != 2) (f[l][r][2][0] += f[l + 1][r - 1][i][j]) %= mod;
                if(j != 1) (f[l][r][0][1] += f[l + 1][r - 1][i][j]) %= mod;
                if(j != 2) (f[l][r][0][2] += f[l + 1][r - 1][i][j]) %= mod;
            }
    }
    else {
        dfs(l, match[l]); dfs(match[l] + 1, r);
        for(int i = 0;i <= 2; i++) 
            for(int j = 0;j <= 2; j++) 
                for(int p = 0;p <= 2; p++) 
                    for(int q = 0;q <= 2; q++) {
                        if((j == 1 && p == 1) || (j == 2 && p == 2)) continue;
                        (f[l][r][i][q] += 1ll * f[l][match[l]][i][j] * f[match[l] + 1][r][p][q] % mod) %= mod;
                    }
    }
}

int main() {

    cin >> ch + 1;
    len = strlen(ch + 1);
    
    for(int i = 1;i <= len; i++) {
        if(ch[i] == '(') 
            sta[++top] = i;
        if(ch[i] == ')') 
            match[sta[top--]] = i;
    }
    dfs(1, len);
    
    for(int i = 0;i <= 2; i++) 
        for(int j = 0;j <= 2; j++) 
            (ans += f[1][len][i][j]) %= mod;
    printf("%lld", ans);
    
    return 0;
}
posted @ 2020-09-09 20:56  C锥  阅读(138)  评论(0编辑  收藏  举报