poj 2955 Brackets (区间dp)||(记忆话搜素)
来源:互联网 发布:hifi音频播放软件 编辑:程序博客网 时间:2024/05/16 15:25
http://poj.org/problem?id=2955
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
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;typedef long long LL;#define N 175#define INF 0x3f3f3f3f#define PI acos (-1.0)#define EPS 1e-5#define met(a, b) memset (a, b, sizeof (a))char str[N];int dp[N][N];int main (){ while (scanf ("%s", str), strcmp (str, "end")) { met (dp, 0); int len = strlen (str); for (int i=len-1; i>=0; 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 ((str[i] == '(' && str[k] == ')') || (str[i] == '[' && str[k] == ']')) { if (k == i+1) dp[i][j] = max (dp[i][j], dp[k+1][j]+2); else dp[i][j] = max (dp[i][j], dp[i+1][k-1] + dp[k+1][j] + 2); } } } } printf ("%d\n", dp[0][len]); } return 0;}
记忆化搜索
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;typedef long long LL;#define N 175#define INF 0x3f3f3f3f#define PI acos (-1.0)#define EPS 1e-5#define met(a, b) memset (a, b, sizeof (a))char str[N];int dp[N][N];int judge (int sa, int en){ if ((str[sa]=='(' && str[en]==')')|| (str[sa]=='[' && str[en]==']')) return 2; return 0;}int DaTaiyang (int sa, int en){ if (dp[sa][en] != -1) return dp[sa][en]; if (sa >= en) return 0; if (sa+1 == en) return judge (sa, en); dp[sa][en] = DaTaiyang (sa+1, en); for (int i=sa+1; i<=en; i++) if (judge (sa, i)) dp[sa][en] = max (dp[sa][en], DaTaiyang (sa+1, i-1) + DaTaiyang (i+1, en) + 2); return dp[sa][en];}int main (){ while (scanf ("%s", str), strcmp (str, "end")) { met (dp, -1); printf ("%d\n", DaTaiyang (0, strlen(str)-1)); } 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】
- online_judge_1135
- 151221 HAoi2015 总结
- (转载)实现QQ侧滑边栏
- pl/sql 自学软件安装及配置
- 【杭电】[2037]今年暑假不AC
- poj 2955 Brackets (区间dp)||(记忆话搜素)
- mycncart操作使用教程 - 横幅广告
- 20151221留言本程序------struts2和Hibernate的应用总结
- PL/SQL 异常的传播
- dp,px转换
- 搜索引擎基本原理
- early_suspend 休眠死机
- Web Application
- iOS标准时间与时间戳相互转换