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. python 利用正则构建一个计算器

    该计算器主要分为四个模块: weclome_func函数用来进入界面获取表达式,并判断表达式是否正确,然后返回表达式: add_sub函数用来进行加减运算,如果有多个加减运算,会递归,最后返回对应的值 ...

  2. 简单封装get和jsonp

    /** * 向服务器发送GET请求. *  * @param {type} url * @param {type} async 是否异步调用 * @param {type} fnCallback 回调 ...

  3. MyEclipse 2014优化设置(禁用myeclipse updating indexes)

    1.指定本机java环境 Windows-->preferences-->java-->Insetallel JREs 右侧 单击ADD standard VM-->Next ...

  4. 摄像头PIN脚功能作用

    摄像头PIN脚功能作用,Camera硬件系统分析 9 f  E+ E2 b  N. j4 M2 U- a. q9 A) T# c& O& C% x+ l5 l! q           ...

  5. Java伙伴系统(模拟)

    参考:https://labrick.cc/2015/10/12/buddy-system-algorithm/ 代码过烂 不宜参考. output: [operating.entity.Heap@4 ...

  6. RabbitMQ学习之(二)_Centos6下安装RabbitMQ及管理配置

    首先yum方式安装依赖包 yum install ncurses-devel unixODBC unixODBC-devel 安装Erlang语言环境 wget http://erlang.org/d ...

  7. Windows打开软件老是弹出无法验证发布者

    使用组策略管理器 gpedit.msc 用户配置-管理模板-Windows组件-附件管理器-中等风险文件类型的包含列表 .exe;.cmd;.bat;.js即可

  8. Python3.x:定义一个类并且调用

    Python3.x:定义一个类并且调用 1,定一个类Shrjj(其中有属性:name, jjzt,fbsjj,etf,lof,fjlof): class Shrjj(object): def __in ...

  9. 4.9版本的linux内核中实时时钟芯片pt7c4338的驱动源码在哪里

    答:drivers/rtc/rtc-ds1307.c,内核配置项为CONFIG_RTC_DRV_DS1307 Location: -> Device Drivers -> Real Tim ...

  10. [译]JavaScript需要类吗?

    [译]JavaScript需要类吗?   原文:http://www.nczonline.net/blog/2012/10/16/does-javascript-need-classes/ 译者注:在 ...