poj 2955 Brackets 【区间dp 入门】
来源:互联网 发布:大数据分析系统翻译 编辑:程序博客网 时间:2024/04/30 14:05
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
10min1A好激动啊。不知道有没有bug。。。o(╯□╰)o
题意:给定一个只由'[',']','(',')'组成的字符串,问你最大括号匹配的个数。
思路:设置dp[i][j]表示区间[i, j]里面最大括号匹配个数。
首先[i, j]由区间[i+1, j]推导而来,显然有dp[i][j] = dp[i+1][j]。
考虑加入字符str[i]对dp[i][j]的影响。
在from i+1 to j里面找,找到和字符str[i]匹配的字符下标k。
我们把区间[i+1, j]划分为三个子区间[i+1, k-1] [k, k] [k+1, j],状态的转移就出来了。
dp[i][j] = max(dp[i][j], dp[i+1][k-1]+dp[k+1][j]+1)。
AC代码:
#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#define INF 0x3f3f3f#define eps 1e-8#define MAXN (100+1)#define MAXM (100000)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1using namespace std;char str[MAXN];int dp[MAXN][MAXN];bool judge(char a, char b){ return (a == '(' && b == ')') || (a == '[' && b == ']');}int main(){ while(Rs(str), strcmp(str, "end")) { int len = strlen(str); CLR(dp, 0); for(int i = len-1; i >= 0; i--) { for(int j = i; j < len; j++) { dp[i][j] = dp[i+1][j]; for(int k = i+1; k <= j; k++) { if(judge(str[i], str[k])) dp[i][j] = max(dp[i][j], dp[i+1][k-1] + 1 + dp[k+1][j]); } } } Pi(dp[0][len-1]*2); } return 0;}
- 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 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
- python 正则表达式
- 银行家算法
- 设置navigationBar的title的字体大小与颜色
- 黑马程序员之Map
- 吸血鬼数字(一个4位数字,可以拆分2个2位数数字的乘积,顺序不限。)
- poj 2955 Brackets 【区间dp 入门】
- 集合的交并差(链表)(未完)
- Android Studio——点击事件监听
- Java基础的IO和线程知识
- 结构体例题汇总
- 2009年系统分析师上午试题1-2
- 微软亚洲研究院开源图数据库GraphView
- 回调
- memcached-数据结构