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来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
随机推荐
- sass菜鸟教程
1.安装sass 在安装sass前需要安装ruby,下载ruby时要注意自己电脑是32位还是64位,下载好正常安装,但注意一下:要勾选下图这一项,添加环境变量(安装过JDK的朋友应该知道环境变量) 下 ...
- swift学习笔记之-可选链式调用
//可选链式调用 import UIKit /*可选链式调用(Optional Chaining) 1.在可选值上请求和调用该可选值的属性.方法及下标的方法,如果可选值有值,那么调用就会成功,返回可选 ...
- CSS属性之float学习心得
全文参考:http://www.linzenews.com/program/net/2331.html 我们来看看CSS重要属性--float. 以下内容分为如下小节: 1:float属性 2:flo ...
- GP服务将矢量数据加入到栅格数据中的方法
1.如何将矢量数据加入栅格数据中 1. Conversion Tools -->To Raster-->Polygon to Raster 2. Spatial Analyst Tool ...
- Ioc Autofac心得
对于这个容器注入,个人也不是很熟悉,很多还不懂,只会基本的操作,几天把它记录下来,说不定以后帮助就大了呢,这方面跟安卓差距还是挺大的 下面记录下应用的流程 步骤: 1.添加应用 2.重写工厂(这里讲的 ...
- NSString和SwiftString的区别和使用场景
当下Swift项目已经越来越多,可能会经常见到 str as NSString 或者 str as String 字符串在这两者之间的来回切换,因为有些操作用OC字符串比较方便,而有些操作则相反,熟 ...
- android加固系列—1.如何检验so文件是否加壳成功
程序对so文件加壳后,如何验证是否加壳成功呢,首先除了能在应用中正常运行外,还要用IDA来检测: 绿色乱码表示rr这个函数成功加密: 工具下载,可支持动态调试版:
- iOS 设备标识
//UUID NSLog(@"-=-=-=-=-=-=-=-==-=-==qqqqqqqqqqqqqqqqqqqqqqq:::::::::::%@", [[NSUUID UU ...
- iOS 学习 - 9.Block 入门
来自李明杰的视频. block 用来保存一段代码 block 的标志:^ block 跟函数很像: 1).可以保存代码 2).有返回值 3). 有形参 temp1:没有返回值.没有形参的 blo ...
- Unity3D 面试题汇总
最先执行的方法是: 1.(激活时的初始化代码)Awake,2.Start.3.Update[FixUpdate.LateUpdate].4.(渲染模块)OnGUI.5.再向后,就是卸载模块(TearD ...