题目描述
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 ss . 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.
Solution
区间dp好题啊。
设$dp[i][j][3][3]$代表给括号序列$i..j$染色的方案数,后面两维分别代表i和j染什么颜色(具体什么数字代表什么颜色随便,我这里就0是不染色,1是红色,2是蓝色)。
一个括号序列有两种形式:(A),AB。A和B都是合法的括号序列,所以转移时分这两种情况讨论。对于第二种情况,我们只需考虑第一个合法的括号序列和剩下的括号序列。
第一种情况:
记录$g[i][j]$代表i,j是不是一对匹配括号
只有满足时才是情况1,否则为情况2.
可以从dp[i+1][j-1]推到dp[i][j]。
具体的转移方程:
if (k != 1) work(dp[i][j][0][1], dp[i + 1][j - 1][l][k]);
if (k != 2) work(dp[i][j][0][2], dp[i + 1][j - 1][l][k]);
if (l != 1) work(dp[i][j][1][0], dp[i + 1][j - 1][l][k]);
if (l != 2) work(dp[i][j][2][0], dp[i + 1][j - 1][l][k]);
work是将后面的值加给前面,当然len=2时要特判。
第二种情况:
记录一个match[i]代表在i的匹配括号(i是个左括号)。
有转移方程:
work(dp[i][j][l][k], dp[i][match[i]][l][p] * dp[match[i] + 1][j][q][k] % mod);
l,k,q,k都是枚举的颜色。
要满足$p e q$。
Code
#include <iostream> #include <cstdio> #include <cstring> #include <stack> using namespace std; typedef long long ll; const int N = 710; const ll mod = 1e9 + 7; bool g[N][N]; //g[i][j]代表i,j是否匹配 ll dp[N][N][3][3]; //dp[i][j][k][l]代表区间[i,j],i染颜色k,j染颜色l的方案数 //0: 不染色;1:红色;2:蓝色 int n; ll ans; char s[N]; stack<int> stk; int match[N]; void work(ll &x, ll y) { x = (x + y) % mod; } int main() { scanf("%s", s + 1); n = strlen(s + 1); for (int i = 1; i <= n; i++) { if (s[i] == '(') stk.push(i); else g[stk.top()][i] = 1, match[stk.top()] = i, stk.pop(); dp[i][i][0][0] = 1; dp[i][i][1][1] = 1; dp[i][i][2][2] = 1; } for (int len = 2; len <= n; len++) { for (int i = 1; i + len - 1 <= n; i++) { int j = i + len - 1; if (g[i][j]) { if (len > 2) { for (int l = 0; l < 3; l++) { for (int k = 0; k < 3; k++) { if (k != 1) work(dp[i][j][0][1], dp[i + 1][j - 1][l][k]); if (k != 2) work(dp[i][j][0][2], dp[i + 1][j - 1][l][k]); if (l != 1) work(dp[i][j][1][0], dp[i + 1][j - 1][l][k]); if (l != 2) work(dp[i][j][2][0], dp[i + 1][j - 1][l][k]); } } } else { dp[i][j][1][0] = 1; dp[i][j][2][0] = 1; dp[i][j][0][1] = 1; dp[i][j][0][2] = 1; } } else { for (int l = 0; l < 3; l++) { for (int k = 0; k < 3; k++) { for (int p = 0; p < 3; p++) { for (int q = 0; q < 3; q++) { if (p == q && p != 0) continue; work(dp[i][j][l][k], dp[i][match[i]][l][p] * dp[match[i] + 1][j][q][k] % mod); } } } } } } } for (int l = 0; l < 3; l++) { for (int k = 0; k < 3; k++) { work(ans, dp[1][n][l][k]); } } printf("%lld", ans); return 0; }