Brackets
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 i1i2, …, im where 1 ≤ i1 < i2 < … < im ≤ nai1ai2 … 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

Stanford Local 2004

题意:求最大括号匹配

f[i][j]表示i到j的最大括号匹配
两种转移
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=105;
char s[N];
int f[N][N];
inline bool check(int i,int j){if(s[i]=='['&&s[j]==']') return 1;if(s[i]=='('&&s[j]==')') return 1;return 0;
}
int main(){while(~scanf("%s",s+1)){if(s[1]=='e') break;memset(f,0,sizeof(f));int n=strlen(s+1);for(int i=n;i>=1;i--)for(int j=i+1;j<=n;j++){if(check(i,j)) f[i][j]=f[i+1][j-1]+2;for(int k=i;k<=j;k++) f[i][j]=max(f[i][j],f[i][k]+f[k][j]); }printf("%d\n",f[1][n]);        }
}

POJ2955Brackets[区间DP]相关推荐

  1. poj2955Brackets(区间DP)

    Description We give the following inductive definition of a "regular brackets" sequence: t ...

  2. POJ 2955 Brackets (区间DP)

    题目链接:http://poj.org/problem?id=2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  3. 0x53. 动态规划 - 区间DP(习题详解 × 8)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 文章目录 0x53. 动态规划 - 区间DP Problem A. 最优矩阵链乘 Problem B. ...

  4. UVA1626 括号序列 Brackets sequence(区间DP匹配括号,输出匹配方案)

    整理的算法模板合集: ACM模板 UVA1626 Brackets sequence 我们将正规括号序列定义如下: 空序列是正规括号序列. 如果 SSS 是一个正规括号序列,那么 (S) 和 [S] ...

  5. UVA10003 切木棍 Cutting Sticks(区间DP、细节)

    整理的算法模板合集: ACM模板 本题其实就是一个区间DP 的模板题,总长度为len,有n个切割点,也就是说能被切割成n+1段,所以左边界是0,有边界是n + 1,所以答案就是f[0][n + 1]. ...

  6. 【动态规划】区间DP - 最优矩阵链乘(另附POJ1651Multiplication Puzzle)

    最优矩阵链乘(动态规划) 一个n∗mn*mn∗m的矩阵由 nnn 行 mmm 列共 n∗mn*mn∗m 排列而成.两个矩阵A和B可以相乘当且仅当A的列数等于B的行数.一个nm的矩阵乘mp的矩阵,运算量 ...

  7. 【每日DP】day13、P3147 [USACO16OPEN]262144 (区间DP,2048游戏)难度⭐⭐⭐★

    P3147 [USACO16OPEN]262144 P 想到合并,自然就想到区间dp,一个被合成的数之前是一个区间,并且由两个数比它小 111 的区间合成.可麻烦的是,我们并不知道之前的两个区间长度各 ...

  8. poj1651(区间dp)

    题目连接:http://poj.org/problem?id=1651 题意:给出一组N个数,每次从中抽出一个数(第一和最后一个不能抽),该次的得分即为抽出的数与相邻两个数的乘积.直到只剩下首尾两个数 ...

  9. HDU 5115 Dire Wolf ——(区间DP)

    比赛的时候以为很难,其实就是一个区间DP= =..思路见:点我. 区间DP一定要记住先枚举区间长度啊= =~!因为区间dp都是由短的区间更新长的区间的,所以先把短的区间更新完.. 代码如下: 1 #i ...

最新文章

  1. t-top 命令详解
  2. Win7旗舰版中的IIS配置asp.net的运行环境
  3. linux删除sde服务,在Oracle数据库中部署多个SDE服务
  4. ASP.NET Core 登录登出 - ASP.NET Core 基础教程 - 简单教程,简单编程
  5. linux fedora35安装kvm虚拟机命令整理
  6. 关于竖表转横表的问题
  7. android 模拟长按菜单键_如何采用PLC梯形图实现单键启动程序
  8. 但有很多的HTML5朋友
  9. vue和小程序哪个好学一点_litemall,Spring Boot后端,微信小程序用户前端 + Vue用户移动端...
  10. javaweb功能模块如何合理设计_燃油燃气锅炉烟道如何设计才更合理?
  11. html中的灵动标签,《帝国网站管理系统》一招鲜吃天遍天系列教程之 灵动标签使用...
  12. 最新Discuz验证码和PhpWind验证码的识别
  13. Mybatis缓存机制及mybatis的各个组成部分
  14. dontshrink解决ProGuard错误:java.lang.StackOverflowError
  15. paip.svn不能忽略文件的bug.txt
  16. 阿里这份Java程序性能优化指南,让你的程序快上200%
  17. 正规word文档文件字体排版格式要求(标准)
  18. Codeforces 1180B
  19. Financial crime in times of Covid-19 – AML and cyber resilience measures 整理
  20. 使用工具清理Windows的winsxs目录

热门文章

  1. 粤港澳大湾区落地首家人工智能工程院
  2. Facebook的首席技术官:人工智能已用于内容审核,未来会做更多
  3. DeepMind论文:深度压缩感知,新框架提升GAN性能
  4. 对话式AI 2019年遇来的新市场
  5. (已解决)linux如何删除-开头的文件或者目录
  6. vscode使用markdown语言编写文本
  7. 科技巨头纷纷从刷脸转向“刷手”,指纹如何扛起生物识别大旗?
  8. 脑科学研究:对于学习来说,休息可能与练习同样重要...
  9. 一文看懂谷歌的AI芯片布局,边缘端TPU将大发神威
  10. 亚马逊:从零售商向科技公司的质变