POJ2955Brackets[区间DP]
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 6585 | Accepted: 3534 |
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
Source
题意:求最大括号匹配
f[i][j]表示i到j的最大括号匹配
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=;
char s[N];
int f[N][N];
inline bool check(int i,int j){
if(s[i]=='['&&s[j]==']') return ;
if(s[i]=='('&&s[j]==')') return ;
return ;
}
int main(){
while(~scanf("%s",s+)){
if(s[]=='e') break;
memset(f,,sizeof(f));
int n=strlen(s+);
for(int i=n;i>=;i--)
for(int j=i+;j<=n;j++){
if(check(i,j)) f[i][j]=f[i+][j-]+;
for(int k=i;k<=j;k++) f[i][j]=max(f[i][j],f[i][k]+f[k][j]);
}
printf("%d\n",f[][n]);
}
}
POJ2955Brackets[区间DP]的更多相关文章
- POJ2955--Brackets 区间DP入门 括号匹配
题意很简单,就是求给出串中最大的括号匹配数目.基础题,格式基本为简单区间dp模板. #include<iostream> #include<string.h> using na ...
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- BZOJ1055: [HAOI2008]玩具取名[区间DP]
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1588 Solved: 925[Submit][Statu ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
- BZOJ 1260&UVa 4394 区间DP
题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...
- 区间dp总结篇
前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
随机推荐
- Android Handler机制(一)---Message源码分析
Message: 定义: public final class Message implements Parcelable Message类是个final类,就是说不能被继承,同时Message类实现 ...
- Android 显示 WebView ,加载URL 时,向webview的 header 里面传递参数
1.主要布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and ...
- spring.net (1) 概念-控制反转(又名依赖注入)
Spring.net 作为一个应用程序框架,在构建企业级.net应用程序提供了很多灵活而又丰富的功能(如:依赖注入,aop,数据访问抽象,asp.net 扩展). 控制反转: Inversion of ...
- ios 颜色转图片
- (UIImage *)imageWithColor:(UIColor*) color{ CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); ...
- 通过系统架构漏洞获取系统VIP资源
首先说我的构思: 一本小说,有很多集,每一集请求下载都会生成一个k的json,例如: 有了这个k我们就可以定位到这一集具体的位置,这本小说是固定的id,每一集的K找到了,剩下的不就简单了. 再通过抓包 ...
- jenkins打包成功,部署失败
环境一直正常,更新了tomcat版本后自动部署报错 ERROR: Publisher hudson.plugins.deploy.DeployPublisher aborted due to exce ...
- mysql动态行转列
测试数据 DROP TABLE IF EXISTS `score`; CREATE TABLE `score` ( `id` ) NOT NULL AUTO_INCREMENT, `class` ) ...
- H264解码学习-2015.04.16
今天看了不少,却感觉收获寥寥. 1.H264相关知识 因为RTP协议发过来的数据已经经过了H264编码,所以这边需要解码.补充一下H264的相关知识. 与以往的视频压缩标准相比,H.264 视频压缩标 ...
- Join的表顺序
在今天的文章里,我想谈下SQL Server里一个非常有趣的话题:在表联接里,把表指定顺序的话是否有意义?每次我进行查询和性能调优的展示时,大家都会问我他们是否应该把联接中的表指定下顺序,是否会帮助查 ...
- spring定时器(二)
此定时器可重置定时时间. 1. spring的定时器配置文件application.xml: <?xml version="1.0" encoding="UTF-8 ...