POJ 2955-Brackets(区间DP)
来源:互联网 发布:淘宝店怎么更改域名 编辑:程序博客网 时间:2024/05/06 01:09
Description
We give the following inductive definition of a “regular brackets” sequence:
- the empty sequence is a regular brackets sequence,
- if s is a regular brackets sequence, then (s) and [s] are regular brackets sequences, and
- if a and b are regular brackets sequences, then ab is a regular brackets sequence.
- no other sequence is a regular brackets sequence
For instance, all of the following character sequences are regular brackets sequences:
(), [], (()), ()[], ()[()]
while the following character sequences are not:
(, ], )(, ([)], ([(]
Given a brackets sequence of characters a1a2 … an, your goal is to find the length of the longest regular brackets sequence that is a subsequence of s. That is, you wish to find the largest m such that for indices i1, i2, …, imwhere 1 ≤ i1 < i2 < … < im ≤ n, ai1ai2 … aim is a regular brackets sequence.
Given the initial sequence ([([]])]
, the longest regular brackets subsequence is [([])]
.
Input
The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters (
, )
, [
, and ]
; each input test will have length between 1 and 100, inclusive. The end-of-file is marked by a line containing the word “end” and should not be processed.
Output
For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.
Sample Input
((()))()()()([]]))[)(([][][)end
Sample Output
66406
经典区间DP。
题意:求括号匹配数。
dp[l][r]为区间[l,r]内括号匹配数,dp[l][r]=max(dp[l][r],dp[l+1][k-1]+dp[k+1][r]+2)(s[l]和s[k]匹配)
枚举区间,形式大都一样,3层for循环,看到很多人说区间DP写成记忆化搜索比较容易懂。。orz
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <cctype>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>#include <list>#define ll __int64#define pp pair<int,int>using namespace std;const int INF = 0x3f3f3f3f;char s[110];int dp[110][110];void solve(){memset(dp, 0, sizeof(dp));int len = strlen(s) - 1;for (int p = 1; p <= len; p++) {for (int l = 1; l <= len; l++) {int r = l + p - 1;if (r > len) {break;}dp[l][r] = dp[l + 1][r];for (int k = l + 1; k <= r; k++)if ((s[l] == '(' && s[k] == ')') || (s[l] == '[' && s[k] == ']')) {dp[l][r] = max(dp[l][r], dp[l + 1][k - 1] + dp[k + 1][r] + 2);}}}printf("%d\n", dp[1][len]);}int main(){while (~scanf("%s", s + 1) && s[1] != 'e') {s[0] = 2;solve();}return 0;}
- Brackets(poj-2955)(区间dp)
- POJ 2955-Brackets(区间DP)
- poj 2955 Brackets(区间DP)
- POJ 题目2955 Brackets(区间dp)
- poj 2955 Brackets(区间dp)
- POJ 2955 Brackets (区间DP)
- poj 2955 Brackets(区间dp)
- poj 2955 Brackets (区间dp)
- POJ - 2955 Brackets(区间dp)
- POJ 2955 Brackets(区间DP水题)
- POJ 2955 Brackets(区间DP)
- poj--2955--Brackets(区间dp)
- POJ 2955Brackets(区间DP)
- POJ 2955Brackets(区间DP)
- poj 2955 Brackets (区间DP)
- 【POJ 2955】Brackets(区间DP)
- POJ 2955Brackets(区间dp)
- POJ 2955 Brackets (区间DP)
- Redis 数据结构解析和命令指南
- 利用WebView加载HTML代码时解决图片正常显示
- 用Mahout构建职位推荐引擎【一起学Mahout】
- Unity 安装及自带demo运行
- JavaBean命名规范
- POJ 2955-Brackets(区间DP)
- latex行溢出问题 Overfull \hbox (1.1499pt too wide) in paragraph
- Java开发人员最常用19个Linux命令
- 排序算法(1)——看最多节目
- slub中的kmalloc和kfree学习笔记
- Java开发人员最常用19个Linux命令
- js 字符串转换成数字的三种方法
- 语法糖---C++的运算符重载
- [译]Kinect for Windows SDK开发入门(一):开发环境配置