Brackets
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7795   Accepted: 4136

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

 
题解:
f[i][j]表示i到j的最大括号匹配数
 
#include <iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<deque>
#include<algorithm>
#include<string>
#include<stack>
#include<cmath>
using namespace std;
char ch[];
int dp[][];
int n;
bool ok(int x,int y)
{
if (ch[x]=='(' && ch[y]==')') return ;
if (ch[x]=='[' && ch[y]==']') return ;
return ;
} int main()
{
while(~scanf("%s",&ch))
{
if (ch[]=='e') break;
n=strlen(ch);
memset(dp,,sizeof(dp));
// for(int i=0;i<n;i++)
// for(int j=i+1;j<n;j++) 正就是不对的
for(int i=n-;i>=;i--)
for(int j=i+;j<n;j++)
{
if (ok(i,j)) dp[i][j]=max(dp[i][j],dp[i+][j-]+);
for(int k=i;k<=j;k++)
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]); }
printf("%d\n",dp[][n-]); }
return ;
}

Poj 2955 brackets(区间dp)的更多相关文章

  1. HOJ 1936&POJ 2955 Brackets(区间DP)

    Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...

  2. poj 2955 Brackets (区间dp基础题)

    We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...

  3. poj 2955"Brackets"(区间DP)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给你一个只由 '(' , ')' , '[' , ']' 组成的字符串s[ ], ...

  4. poj 2955 Brackets (区间dp 括号匹配)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  5. POJ 2955 Brackets 区间DP 入门

    dp[i][j]代表i->j区间内最多的合法括号数 if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']') dp[i][j] ...

  6. POJ 2955 Brackets(区间DP)

    题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <vector& ...

  7. POJ 2955 Brackets 区间DP 最大括号匹配

    http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...

  8. POJ 2995 Brackets 区间DP

    POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...

  9. A - Brackets POJ - 2955 (区间DP模板题)

    题目链接:https://cn.vjudge.net/contest/276243#problem/A 题目大意:给你一个字符串,让你求出字符串的最长匹配子串. 具体思路:三个for循环暴力,对于一个 ...

  10. POJ 2955 Brackets 区间合并

    输出一个串里面能匹配的括号数 状态转移方程: if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']')             dp ...

随机推荐

  1. 开源一款android 偷拍 app【静拍】豌豆荚、flyme商店已经上线

    首先先花3秒时间,预览下下app的大概是做啥的,解决啥痛点的:) app: 本地下载地址 需求点: 1:音量键可以拍照 2:没有快门声.闪光灯 3:锁屏下.或者是在其他程序界面都可以拍照 思路: 1: ...

  2. JQuery EasyUI 扩展方法 日期控件 设置时间段函数

    /** Jquery扩展方法--by hgx 2018年1月8日-- * 设置时间段函数,开始时间(1号)与结束时间(当前日期) * 传入参数:--spaceMonth:查询间隔月,1为间隔查询一个月 ...

  3. fir.im 测试包下载工具--FIRReader 的图文介绍

    fir.im 是一个供开发者上传测试包(也可以是企业的正式包)的网站.该网站提供了接口访问应用资源.下面要介绍的这个应用对接了这些接口,供用户方便下载应用. 先来看下应用截图吧! 主要有以下功能点:1 ...

  4. CSS Display(显示)和Visibility(可见性)

    CSS Display(显示)和Visibility(可见性) 一.简介 display属性设置一个元素应如何显示(隐藏不占用空间),visibility属性指定一个元素应可见还是隐藏(隐藏占用空间) ...

  5. 20145314郑凯杰 《Java程序设计》课程总结

    20145314郑凯杰 <Java程序设计>课程总结 每周读书笔记链接汇总 ①寒假预习--"helloworld" ②第一周读书笔记 ③第二周读书笔记 ④第三周读书笔记 ...

  6. 推荐一个快速了解移植uboot以及linux到新板子上的ppt教程

    链接地址在此: https://elinux.org/images/2/2a/Schulz-how-to-support-new-board-u-boot-linux.pdf

  7. shell脚本中使用什么工具进行计算

    1.答: expr 2. expr的用法: jello=$(expr 1 \* 3) //乘法,注意1和expr之间有空格,1与转换符\之间有空格,3和*之间有空格 jello=$(expr 1 / ...

  8. svn的分支

    svn的分支使用 新建一个项目的时候,选择建立自带trunk,branches和tags文件夹的. 其中trunk作为主开发. 有需要的时候,从trunk创建分支到对应的branches下面,新建分支 ...

  9. C# 往string [] arr 数组插入元素

    string [] arr ; List<string> _list = new List<string>(arr ); for(int i ;i<10;i++) { _ ...

  10. from: can't read /var/mail/xxx 解决方法

    在执行一个发包脚本的时候,遇到了如下问题: from: can't read /var/mail/scapy.all 原因:脚本是没有问题的,但它并不是可以被python执行的可执行文件. 解决方法: ...