Brackets

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 14226 Accepted: 7476

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 i1, i2, …, im where 1 ≤ i1 < i2 < … < im ≤ n, ai1ai2 … 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

#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
bool match(char a,char b);
#define mst(a,b) memset((a),(b),sizeof(a))
const int maxn=500;
int dp[maxn][maxn];
int main()
{
string ob;
while(cin>>ob)
{
if(ob=="end") break;
mst(dp,0);
for(int len=2;len<=ob.length( );len++){
for(int i=1;i<=ob.length( )+1-len;i++){
int j=len+i-1;
if(match(ob[i-1],ob[j-1])) dp[i][j]=dp[i+1][j-1]+2;
for(int k=i;k<j;k++)
{
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
}
}
}
// for(int len=1;len<ob.length( )-2;len++) cout<<dp[len][len+3]<<' ';
cout<<dp[1][ob.length( )]<<endl;
}
}
bool match(char a,char b)
{
if(a=='('&&b==')') return 1;
if(a=='['&&b==']') return 1;
else return 0;
}

POJ 2955 区间DP必看的括号匹配问题,经典例题的更多相关文章

  1. POJ 2955 (区间DP)

    题目链接: http://poj.org/problem?id=2955 题目大意:括号匹配.对称的括号匹配数量+2.问最大匹配数. 解题思路: 看起来像个区间问题. DP边界:无.区间间隔为0时,默 ...

  2. poj 2955 区间dp入门题

    第一道自己做出来的区间dp题,兴奋ing,虽然说这题并不难. 从后向前考虑: 状态转移方程:dp[i][j]=dp[i+1][j](i<=j<len); dp[i][j]=Max(dp[i ...

  3. POJ 2955 区间DP Brackets

    求一个括号的最大匹配数,这个题可以和UVa 1626比较着看. 注意题目背景一样,但是所求不一样. 回到这道题上来,设d(i, j)表示子序列Si ~ Sj的字符串中最大匹配数,如果Si 与 Sj能配 ...

  4. 区间dp好题cf149d 括号匹配

    见题解链接https://blog.csdn.net/sdjzping/article/details/19160013 #include<bits/stdc++.h> using nam ...

  5. 区间DP(入门)括号匹配

    https://www.nitacm.com/problem_show.php?pid=8314 思路:类似于https://blog.csdn.net/MIKASA3/article/details ...

  6. poj 3280(区间DP)

    Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7869   Accepted: 38 ...

  7. poj 2955 Brackets dp简单题

    //poj 2955 //sep9 #include <iostream> using namespace std; char s[128]; int dp[128][128]; int ...

  8. POJ 1651 (区间DP)

    题目链接: http://poj.org/problem?id=1651 题目大意:加分取牌.如果一张牌左右有牌则可以取出,分数为左牌*中牌*右牌.这样最后肯定还剩2张牌.求一个取牌顺序,使得加分最少 ...

  9. POJ 1141 区间DP

    给一组小括号与中括号的序列,加入最少的字符,使该序列变为合法序列,输出该合法序列. dp[a][b]记录a-b区间内的最小值, mark[a][b]记录该区间的最小值怎样得到. #include &q ...

随机推荐

  1. Scala——的并行集合

    当出现Kafka单个分区数据量很大,但每个分区的数据量很平均的情况时,我们往往采用下面两种方案增加并行度: l  增加Kafka分区数量 l  对拉取过来的数据执行repartition 但是针对这种 ...

  2. Go中的unsafe

    unsafe 最近关注了一个大佬的文章,文章写的非常好,大家可以去关注下. 微信公众号[码农桃花源] 指针类型 我们知道slice 和 map 包含指向底层数据的指针 什么是 unsafe 为什么会有 ...

  3. 一个不错的博客-涉及el 、jstl、log4j 入门等

    http://www.cnblogs.com/Fskjb/category/198224.html

  4. Linux 命令系列之 seq

    简介 seq -- print sequences of numbers seq 命令可以输出各种有规律的数字. 用法 usage: seq [-w] [-f format] [-s string] ...

  5. Python的炫技操作:条件语句的七种写法

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: Python极客社区 PS:如有需要Python学习资料的小伙伴可以 ...

  6. The Super Powers UVA - 11752

    题目大意:将范围从1~pow(2,64)-1内的super power输出.super power的定义:一个数x至少存在两种x=pow(i,k),(k!=1). 题解: 注意数据范围2的64次方-1 ...

  7. centos7 —— 网络连接问题

    今天用虚拟机(VM)安装好centos7后,发现无法连接网络,百思不得其解: 第一步:找到需要修改的文件位置,查明原因 #.查看网络是否可以ping通 ~ ping www.baidu.com #.查 ...

  8. 《闲扯Redis五》List数据类型底层之quicklist

    一.前言 Redis 提供了5种数据类型:String(字符串).Hash(哈希).List(列表).Set(集合).Zset(有序集合),理解每种数据类型的特点对于redis的开发和运维非常重要. ...

  9. (一)微信小程序:实现引导页

    基本目录结构 index目录下文件操作步骤 1.针对index.wxml <!--index.wxml--> <view class="index-container&qu ...

  10. redis: 乐观锁(十)

    监视:watch 正常业务(单线程): 127.0.0.1:6379> set money 100 #模拟存款100元 OK 127.0.0.1:6379> set moneyout 0 ...