Codeforces Round #106 (Div. 2) D. Coloring Brackets —— 区间DP
题目链接:https://vjudge.net/problem/CodeForces-149D
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).
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(109 + 7).
(())
12
(()())
40
()
4
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.
题解:
给出一串合法的括号,为括号上色,有如下原则:1)一对括号有且仅有一个是涂上颜色的, 2)颜色只有两种, 3)相邻的括号的颜色不允许相同(除非都没上色)。问:满足上述三个条件的上色方案有多少种?
1.由于给出的括号序列是合法的,即左括号与右括号一一对应。所以我们可以先预处理出每个左括号所对应的右括号。
2.详情请看代码注释。
写法一(人工枚举):
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const int INF = 2e9; 15 const LL LNF = 9e18; 16 const int MOD = 1e9+7; 17 const int MAXN = 800+10; 18 19 LL dp[MAXN][MAXN][2][2]; 20 int top, Stack[MAXN], match[MAXN]; 21 char s[MAXN]; 22 23 //区间为[l, r], isL为l-1处是否上色, isR为r+1处是否上色。 24 LL dfs(int l, int r, bool isL, bool isR) 25 { 26 if(l>=r) return 1; //因为是乘法,所以遇到非法位置,就返回1。要是加法就return0或者1(实际情况实际考虑)。 27 if(dp[l][r][isL][isR]!=-1) return dp[l][r][isL][isR]; 28 29 LL ret = 0; 30 int k = match[l]; //找到与最左端的左括号匹配的右括号 31 32 if(!isL) //首先考虑为左括号上色。如果l-1处没有上色, 那么左括号就可以上两种颜色 33 ret = (ret + (2LL*dfs(l+1, k-1, true, false)*dfs(k+1, r, false, isR))%MOD)%MOD; 34 else //否则, 左括号只能上一种颜色,与l-1处括号相对的颜色 35 ret = (ret + (1LL*dfs(l+1, k-1, true, false)*dfs(k+1, r, false, isR))%MOD)%MOD; 36 if(k!=r || (k==r&&!isR) ) //其次为右括号上色。如果右括号不在右端点,或者在右端点但是r+1处没有上色,则可上两种颜色 37 ret = (ret + (2LL*dfs(l+1, k-1, false, true)*dfs(k+1, r, true, isR))%MOD)%MOD; 38 else //否则,右括号在最右端且r+1处上了颜色,那么右括号只能上一种颜色。 39 ret = (ret + (1LL*dfs(l+1, k-1, false, true)*dfs(k+1, r, true, isR))%MOD)%MOD; 40 41 return dp[l][r][isL][isR] = ret; 42 } 43 44 int main() 45 { 46 while(scanf("%s", s+1)!=EOF) 47 { 48 int n = strlen(s+1); 49 top = 0; 50 for(int i = 1; i<=n; i++) //为左括号找到匹配的右括号 51 { 52 if(s[i]=='(') Stack[top++] = i; 53 else match[Stack[--top]] = i; 54 } 55 56 memset(dp, -1, sizeof(dp)); 57 dfs(1, n, 0, 0); 58 printf("%lld\n", dp[1][n][0][0]); 59 } 60 }
写法二(for枚举,推荐):
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const int INF = 2e9; 15 const LL LNF = 9e18; 16 const int MOD = 1e9+7; 17 const int MAXN = 800+10; 18 19 LL dp[MAXN][MAXN][3][3]; 20 int top, Stack[MAXN], match[MAXN]; 21 char s[MAXN]; 22 23 //区间为[l, r],Lcol为l-1处的颜色, Rcol为r+1处的颜色,0代表没上色,1和2分别代表两种不同的颜色 24 LL dfs(int l, int r, int Lcol, int Rcol) 25 { 26 if(l>=r) return 1; //因为是乘法,所以遇到非法位置,就返回1。要是加法就return0或者1(实际情况实际考虑)。 27 if(dp[l][r][Lcol][Rcol]!=-1) return dp[l][r][Lcol][Rcol]; 28 29 LL ret = 0; 30 int k = match[l]; //找到与最左端的左括号匹配的右括号 31 for(int lc = 0; lc<3; lc++) //枚举这个括号的着色情况,并且需要去除掉非法的情况 32 for(int rc = 0; rc<3; rc++) 33 { 34 if((lc&&rc)||(!lc&&!rc)) continue; //如果两个括号都没涂色或者都涂上颜色,非法 35 if(lc && lc==Lcol) continue; //如果l-1处涂上了颜色,且l处也要涂上相同的颜色, 非法 36 if(k==r && rc && rc==Rcol) continue; //如果匹配的右括号在最右端,且r+1处涂上了颜色,又尝试为 37 //右括号涂上相同的颜色,非法。如果右括号不在最右端,那么就无需 38 //考虑r+1处的着色情况了,因为右括号右边的括号必定没有上色。 39 ret = (ret+(1LL*dfs(l+1, k-1, lc, rc)*dfs(k+1, r, rc, Rcol))%MOD)%MOD; //统计合法的情况 40 } 41 return dp[l][r][Lcol][Rcol] = ret; 42 } 43 44 int main() 45 { 46 while(scanf("%s", s+1)!=EOF) 47 { 48 int n = strlen(s+1); 49 top = 0; 50 for(int i = 1; i<=n; i++) //为左括号找到匹配的右括号 51 { 52 if(s[i]=='(') Stack[top++] = i; 53 else match[Stack[--top]] = i; 54 } 55 56 memset(dp, -1, sizeof(dp)); 57 printf("%lld\n", dfs(1, n, 0, 0)); 58 } 59 }