poj2955 Brackets--最大括号匹配数
来源:互联网 发布:淘宝怎么申请试用 编辑:程序博客网 时间:2024/06/09 20:18
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, …, im where 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[i][j]表示 i~j 区间内的最大匹配数。首先假设s[i]和s[j]不匹配,s[i][j]=s[i+1][j],然后在区间k(i+1~j)之间寻找s[i]和s[k]匹配,状态转移 dp[i][j]=max(dp[i][j],dp[i][k-1]+dp[k+1][j]+2);
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<algorithm>using namespace std;int dp[110][110];char s[110];int main(){ while(scanf("%s",s+1)==1 && s[1]!='e') { int len = strlen(s+1); memset(dp,0,sizeof(dp)); for(int i=len-1;i>=1;i--) for(int j=i+1;j<=len;j++) { dp[i][j]=dp[i+1][j]; for(int k=i+1;k<=j;k++) { if(s[i]=='('&&s[k]==')' || s[i]=='['&&s[k]==']') dp[i][j] = max( dp[i][j], dp[i+1][k-1]+dp[k+1][j]+2); } } printf("%d\n",dp[1][len]); }}
- poj2955 Brackets--最大括号匹配数
- POJ2955 Brackets(区间DP,括号匹配)
- poj2955(括号匹配DP)
- poj2955,括号匹配,区间dp
- poj2955(括号匹配问题)
- (POJ 2955)Brackets 区间DP 最大括号匹配
- POJ 2955 Brackets(括号最大匹配,区间DP)
- poj2955 Brackets
- 【POJ2955】【Brackets】
- poj2955 Brackets
- POJ2955 Brackets
- POJ2955-Brackets
- poj2955 Brackets
- poj 2955 Brackets(括号匹配)
- POJ2955 Brackets (DP)
- POJ2955:Brackets(区间DP)
- POJ2955 Brackets 动态规划
- poj2955 Brackets 区间dp
- 一年后姑娘顺利出院了。
- 寻找最大的K个数
- 完美解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法
- CDN是到底什么玩意啊?
- 探讨OpenCV中诸如cvCopy(*src,*dst,*mask) 中的mask的作用是什么?
- poj2955 Brackets--最大括号匹配数
- C语言中数据类型转换
- HDU - 4939 Stupid Tower Defense
- InnoDB与Myisam的六大区别总结
- Optimal Milking
- poj 1195 二维树状数组 及二维树状数组模板
- 经典C++ part1
- 求两个字符串的最大公共子串
- word下面画流程图技巧