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. [MCSM] 蒙特卡罗统计方法

    起因 最开始的时候,写多了LDPCC误码率的仿真,心中便越来越有了疑惑.误码率仿真,多为Monte Carlo仿真,其原理是什么,仿真结果是否可靠,可靠程度是多少,如何衡量其可靠性这些问题我都很不清楚 ...

  2. 我从腾讯那“偷了”3000万QQ用户数据,出了份很有趣的独家报告!

    声明: 1.目前程序已停止运行!QQ空间也已升级访问安全机制. 2.本“分析”数据源自部分用户的公开信息,并未触及隐私内容,广大网友无需担心. 3.QQ空间会不定期发布大数据分析报告,感兴趣的朋友关注 ...

  3. Windows Phone8 中如何引用 SQLite 数据库

    SQLite数据库介绍 1.SQLite是一款轻型的嵌入式数据库,使用C++开发,使用非常广泛 2.SQLite是一款跨平台的数据库,支持Windows.Linux.Android.IOS.Windo ...

  4. 年终福利:调试.NET Framework源代码

    前言 要问JAVA语言最大的优势之一,那就是开源.开源的JAVA框架让JAVA程序员可以不断的通过看源代码来学习.成长.解决问题.并随着时间增长能力越来越强,自然薪水就越来越高.而DONET程序员要看 ...

  5. angular的splitter案例学习

    angular的splitter案例学习,都有注释了,作为自己的备忘. <!DOCTYPE html> <html ng-app="APP"> <he ...

  6. Struts2+Jquery实现ajax并返回json类型数据

    来源于:http://my.oschina.net/simpleton/blog/139212 摘要 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的 ...

  7. iOS边练边学--AFNetWorking框架GET、Post、Download、Upload,数据解析模式以及监控联网状态

    一.AFNETWorking简单使用 get请求 get请求,以后经常用NSURLSession底层的写的部分 简单的post请求 用post请求下载文件,方法很多,还可以通过upload任务来执行 ...

  8. 输入年月,输出月份有几天(分别用了if——else和switch)

    首先是switch做的 class Program { static void Main(string[] args) {/* 题目要求:请用户输入年份,输入月份,输出该月的天数. 思路:一年中月份的 ...

  9. 嘻哈帮天通苑_poppin——张锋

    l click_me

  10. 使用quartz 定时任务

    Quartz 是一个开源的作业调度框架,它完全由 Java 写成,并设计用于 J2SE 和 J2EE 应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它来为执行一个作业而创建简单的或复杂的调度. ...