poj 2955 Brackets(区间dp)
2015-01-05 21:46
465 查看
题目链接
Brackets
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
Input
The input test file will contain multiple test cases. Each input test case consists of a single line containing only the characters
Output
For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line.
Sample Input
Sample Output
Source
题意:给一个只包含括号的串,问该串的最长的合法括号匹配的子序列的长度?
题解:先考虑第一个括号,有两种操作:删除这个括号;将该括号与另一个括号匹配。
用dp[l][r]表示区间[l,r]的最长合法子序列的长度,转移就是:
1,删除第一个括号
dp[l][r]=dp[l+1][r]
2,将第一个括号和第i个括号匹配
dp[l][r]=min(dp[l+1][i-1]+dp[i+1][r]+2);
代码如下:
Brackets
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3470 | Accepted: 1792 |
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
6 6 4 0 6
Source
题意:给一个只包含括号的串,问该串的最长的合法括号匹配的子序列的长度?
题解:先考虑第一个括号,有两种操作:删除这个括号;将该括号与另一个括号匹配。
用dp[l][r]表示区间[l,r]的最长合法子序列的长度,转移就是:
1,删除第一个括号
dp[l][r]=dp[l+1][r]
2,将第一个括号和第i个括号匹配
dp[l][r]=min(dp[l+1][i-1]+dp[i+1][r]+2);
代码如下:
#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<string> #include<math.h> #define nn 110 #define inff 0x7fffffff #define eps 1e-8 typedef long long LL; const LL inf64=LL(inff)*inff; using namespace std; int n; char s[nn]; int dp[nn][nn]; int lj[nn][nn]; bool check(int l,int r) { if(s[l]=='('&&s[r]==')') return true; if(s[l]=='['&&s[r]==']') return true; return false; } int dfs(int l,int r) { if(dp[l][r]!=-1) return dp[l][r]; if(l>r) return 0; if(l==r) return dp[l][r]=0; dp[l][r]=0; int i; for(i=l+1;i<=r;i++) { if(check(l,i)) dp[l][r]=max(dp[l][r],dfs(l+1,i-1)+dfs(i+1,r)+2); } dp[l][r]=max(dp[l][r],dfs(l+1,r)); return dp[l][r]; } int main() { int i; while(scanf("%s",s)!=EOF) { if(strcmp(s,"end")==0) break; int len=strlen(s); memset(dp,-1,sizeof(dp)); printf("%d\n",dfs(0,len-1)); } return 0; }
相关文章推荐
- poj 2955 Brackets(括号匹配,区间DP)
- POJ 2955 Brackets (区间DP)
- poj 2955 Brackets(区间DP)
- POJ - 2955Brackets(区间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 入门
- POJ2955——Brackets(区间dp)
- poj 2955 Brackets 区间dp
- POJ 2955 Brackets (区间dp)
- POJ 2955 Brackets【区间DP】
- 【POJ 2955】【经典区间DP 递推写法】 Brackets 【合法括号匹配成功结果+2,求最大结果】
- poj 2955 Brackets( 区间dp )
- poj 2955 Brackets 区间dp
- POJ 2955 Brackets & POJ 1505 Copying Books & POJ 1651 Multiplication Puzzle(初级区间DP)