Codeforces-149D-Coloring Brackets【区间DP】
来源:互联网 发布:工作软件开发经历 编辑:程序博客网 时间:2024/04/29 16:26
题目:
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).
input
(())
output
12
input
(()())
output
40
input
()
output
4
题目链接:cf-149D
题目大意:
给一个给定括号序列,给该括号上色。
上色规则:1.只能选择红色、蓝色、无色2.每对括号,只能给其中一个上色3.相邻两个颜色不可相同,但可以都为无色
题目思路:区间dp
0:无色; 1:红色; 2:蓝色
dp[i][j][p][q] //表示i-j这个区间,i涂p颜色,j涂q颜色的方案数
初始化:如果 l = r - 1;则表示只有一对括号
dp[l][r][0][1] = 1; dp[l][r][1][0] = 1; dp[l][r][0][2] = 1; dp[l][r][2][0] = 1;
如果 l 和 r 是一对括号
dp[l][r][0][1] += dp[l + 1][r - 1][i][j]dp[l][r][0][2] += dp[l + 1][r - 1][i][j]dp[l][r][1][0] += dp[l + 1][r - 1][i][j]dp[l][r][2][0] += dp[l + 1][r - 1][i][j]
如果不是,
dp[l][r][i][j] += dp[l][k][i][q] * dp[k + 1][r][p][j]//k:和l配对的括号的位置;q:k所选的颜色;p:k+1所选的颜色
以下是代码:
#include <iostream>#include <iomanip>#include <fstream>#include <sstream>#include <cmath>#include <cstdio>#include <cstring>#include <cctype>#include <algorithm>#include <functional>#include <numeric>#include <string>#include <set>#include <map>#include <stack>#include <vector>#include <queue>#include <deque>#include <list>using namespace std;#define mod 1000000007#define maxn 705long long dp[maxn][maxn][3][3]; //i到j区间,两端分别染成q,p颜色的方法数。q(0~3)int match[maxn];string s;void getmatch(){ int len = s.size(); stack<int> st; for (int i = 0; i < len; i++) { if (s[i] == '(') st.push(i); else { match[i] = st.top(); match[st.top()] = i; st.pop(); } }}void solve(int l, int r){ if (l == r - 1) { dp[l][r][0][1] = 1; dp[l][r][1][0] = 1; dp[l][r][0][2] = 1; dp[l][r][2][0] = 1; return; } if (match[l] == r) { solve(l + 1, r - 1); for(int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j != 1) dp[l][r][0][1] = (dp[l][r][0][1] + dp[l + 1][r - 1][i][j]) % mod; if (j != 2) dp[l][r][0][2] = (dp[l][r][0][2] + dp[l + 1][r - 1][i][j]) % mod; if (i != 1) dp[l][r][1][0] = (dp[l][r][1][0] + dp[l + 1][r - 1][i][j]) % mod; if (i != 2) dp[l][r][2][0] = (dp[l][r][2][0] + dp[l + 1][r - 1][i][j]) % mod; } } } else { int k = match[l]; //找到与l匹配的点,记为k solve(l,k); //左边 solve(k + 1, r); //右边 for (int i = 0; i < 3; i++) //l的颜色 { for (int j = 0; j < 3; j++) //r的颜色 { for (int q = 0; q < 3; q++) //k的颜色 { for (int p = 0; p < 3; p++) //k+1的颜色 { if ((q == 1 || q == 2) && (q == p)) continue; dp[l][r][i][j] = (dp[l][r][i][j] + (dp[l][k][i][q] * dp[k + 1][r][p][j])% mod) % mod; } } } } }}int main(){ while(cin >> s) { memset(dp, 0, sizeof dp); int len = s.size(); getmatch(); solve(0,len - 1); long long ans = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) ans = (ans + dp[0][len - 1][i][j]) % mod; cout << ans << endl; } return 0;}
- CodeForces - 149D Coloring Brackets[区间dp]
- codeforces 149D - Coloring Brackets (区间dp)
- CodeForces 149D Coloring Brackets(区间DP)
- CodeForces - 149D Coloring Brackets(区间DP)
- Codeforces 149D Coloring Brackets 【区间dp】
- CodeForces 149D Coloring Brackets(区间DP)
- codeforces 149D Coloring Brackets(区间dp)
- CodeForces 149D Coloring Brackets 区间DP
- CodeForces-149D Coloring Brackets(区间dp)
- codeforces 149D Coloring Brackets 区间DP
- Codeforces-149D-Coloring Brackets【区间DP】
- Codeforces 149D Coloring Brackets(区间dp)
- Codeforces 149D - Coloring Brackets(区间DP)
- CodeForces 149D Coloring Brackets(区间DP+dfs)
- Codeforces 149D Coloring Brackets (不错的区间DP)
- Codeforces 149 D Coloring Brackets(区间dp,标记状态,dfs)
- CodeForces 149 D.Coloring Brackets(区间DP)
- CodeForces 149D Coloring Brackets(区间DP)
- 读书记录
- 继承
- html5的canvas时钟
- 云服务器评测--BAT(阿里云、腾讯云、百度云)云服务器哪家强?
- Bicycle Race
- Codeforces-149D-Coloring Brackets【区间DP】
- Python初学者的17个技巧
- 关于js原型继承的理解
- KMP算法
- 最大流最小割(max flow min cut)
- 日新进用户200W+,解密《龙之谷》手游背后的压测故事
- MySQL引擎介绍ISAM,MyISAM,HEAP,InnoDB
- Android Hook 步骤
- idea 导出javadoc