poj2955——Brackets(区间dp)
2016-05-13 17:00
453 查看
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
6
6
4
0
6
设dp[i][j]为区间i~j的最大能匹配数量。先初始化,看相邻两个能否匹配,接着隔一个匹配,隔两个匹配。。。之后的匹配要选择是否比两个子区间相连的数量更大
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
设dp[i][j]为区间i~j的最大能匹配数量。先初始化,看相邻两个能否匹配,接着隔一个匹配,隔两个匹配。。。之后的匹配要选择是否比两个子区间相连的数量更大
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #define MAXN 105 using namespace std; char s[MAXN]; int dp[MAXN][MAXN]; bool check(int a,int b) { if((a=='['&&b==']')||(a=='('&&b==')')) return true; return false; } int main() { int i,j,k,len; while(gets(s)) { memset(dp,0,sizeof(dp)); if(s[0]=='e') break; len=strlen(s); for(i=0; i<len; ++i) { if(check(s[i],s[i+1])) dp[i][i+1]=2; else dp[i][i+1]=0; } for(int gap=3; gap<=len; ++gap) for(i=0; i+gap-1<len; ++i) { j=i+gap-1; if(check(s[i],s[j])) dp[i][j]=dp[i+1][j-1]+2; for(k=i; k<j; ++k) dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]); } cout<<dp[0][len-1]<<endl; } return 0; }
相关文章推荐
- 原生js和jquery分别实现横向导航菜单效果
- 前端开发课程学习初体验
- 获取代理 原客户端ip
- Hive教程
- 解决之前App内语言切换的某些bug
- MySQL的自然联结+外部联结(左外连接,右外连接)+内部联结
- DWR 消息推送
- 网络扫盲——NAT(网络地址转换)
- Linux下项目与事务跟踪工具JIRA搭建汉化安装详解
- C++的常量引用
- android SeekBar的自定义样式设置
- Jetty的安装和配置
- java中文乱码解决之道(二)—–字符编码详解:基础知识 + ASCII + GB**
- Android中activity保存状态和数据到底该在哪个方法中进行
- Could not obtain transaction-synchronized Session for current thread
- 读nandflash详解
- 判断两条直线是否相交
- java 反编译和引用jar包的方法
- 【搞定配色】超实用!帮你快速搞定网页配色的方法(附配色神器)
- 【BZOJ4439】[Swerc2015]Landscaping【最小割】