Brackets
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3624   Accepted: 1879

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, …, imwhere 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

 #include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
char st[] ;
int a[][] ; bool check (int a , int b)
{
if (st[a] == '(' && st[b] == ')' )
return ;
if (st[a] == '[' && st[b] == ']' )
return ;
return ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
while () {
gets (st) ;
if (strcmp (st , "end") == )
break ;
memset (a , , sizeof(a) ) ;
int len = strlen (st) ;
for (int o = ; o <= len ; o++) {
for (int i = ; i < len - o + ; i++) {
int j = i + o ;
for (int k = i ; k < j ; k++ ) {
a[i][j - ] = max (a[i][j - ] , a[i][k] + a[k + ][j - ] ) ;
if (check (i , j - ) ) {
a[i][j - ] = max (a[i][j - ] , a[i + ][j - - ] + ) ;
}
}
}
}
printf ("%d\n" , a[][len - ] ) ;
}
return ;
}

区间dp感觉和merge sort有异曲同工之妙
从可行的最小区间出发,逐级上去,最终得到整段区间的最终结。

dp[i][j] 指[i , j]这段区间的最优解

Brackets(区间dp)的更多相关文章

  1. Codeforces 508E Arthur and Brackets 区间dp

    Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...

  2. POJ 2995 Brackets 区间DP

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

  3. CF149D. Coloring Brackets[区间DP !]

    题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...

  4. POJ2955:Brackets(区间DP)

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

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

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

  6. Code Forces 149DColoring Brackets(区间DP)

     Coloring Brackets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. POJ2955 Brackets —— 区间DP

    题目链接:https://vjudge.net/problem/POJ-2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

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

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

  9. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

随机推荐

  1. 20145222黄亚奇《Java程序设计》第1周学习总结

    教材学习内容总结 BJVM是Java程序唯一认识的操作系统,其可执行文件为.class文档 Java的三大平台为Java SE,Java EE,Java ME. Java SE的四个部分为:JVM,J ...

  2. Linux下高频命令分类辑录(基本使用篇)

    本文目的:总结linux下常用命令的基本使用方法 文件权限: 文档权限设置命令:chmod 数字模式: 文档权限由-rwxrwxrwx十个字符组成,其中第一个代表文档类型,后面九个字符按照顺序分为三组 ...

  3. ubuntu安装 laravel 过程中出现: mcrypt php extension required 的问题 | 以及composer相关问题 | Nginx安装

    这篇文章对于Nginx的配置至关重要 如果碰到访问index.php不返回html而出现下载文件的问题,加上那段default就可以修正: https://www.digitalocean.com/c ...

  4. Use Windows Azure AD to create SSO projects

    Keywords Windows Azure AD, SSO Summary Use Windows Azure AD to create SSO projects Detailed Scenario ...

  5. [AHOI2013]打地鼠(网络流)

     [问题描述]      游戏里一共会冒出来N个地鼠,这些地鼠冒出来的位置都分布在一条直线上.第i个地鼠会在Ti时刻在Xi位置冒出来,打到第i个地鼠的得分是Pi.     当游戏开始时(也就是0时刻) ...

  6. Graphics samples2

    为图形填充渐变色: Graphics2D g2=(Graphics2D)g; GradientPaint gra=new GradientPaint(20, 20, Color.BLUE, 100,8 ...

  7. Java-EnumSet

    如下 package 集合类.Set类; /** * Set不允许重复数据 */ /** * 这个类是1.5开始有的, * 目前个人使用量几乎为零,很少使用 * 其使用方式和普通的Set没有区别,只是 ...

  8. [Asp.net mvc] 在Asp.net mvc 中使用MiniProfiler

    MiniProfiler是Stack Overflow团队设计的一款性能分析的小程序.可以对一个页面本身,及该页面通过直接引用.Ajax.Iframe形式访问的其它页面进行监控,监控内容包括数据库内容 ...

  9. opencv笔记2:图像ROI

    time:2015年 10月 03日 星期六 12:03:45 CST # opencv笔记2:图像ROI ROI ROI意思是Region Of Interests,感兴趣区域,是一个图中的一个子区 ...

  10. BZOJ-1061 志愿者招募 线性规划转最小费用最大流+数学模型 建模

    本来一眼建模,以为傻逼题,然后发现自己傻逼...根本没想到神奇的数学模型..... 1061: [Noi2008]志愿者招募 Time Limit: 20 Sec Memory Limit: 162 ...