Brackets
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6033   Accepted: 3220

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

 
题意:给你一个串 问你有多少可匹配的括号  ‘(’   ')'   '['  ']'
 
题解:dp[i][j] 表示 区间i~j有多少可匹配的括号
状态转移方程:dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]) k(为分界点)
但是如果a[i]与a[j]匹配 还需要增加判断 dp[i][j]=max(dp[i][j],dp[i+1][j-1]+2)
来确保最优。
做了一些区间dp,要注意的几点
1.分界点的枚举
2.边界的处理
3.递推过程中的i,j
4.区间dp就是逆着状态递推(dp不都是这样吗 zz)
 
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<cmath>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
char a[];
int dp[][];
using namespace std;
int main()
{
while(gets(a))
{
if(a[]=='e')
break;
int len=strlen(a);
memset(dp,,sizeof(dp));
/*for(int i=; i<len-; i++)
{
if(((a[i]=='(')&&(a[i+]==')'))||((a[i]=='[')&&(a[i+]==']')))//边界处理
dp[i][i+]=;
}*/此处删去仍然可以 ac 细细想一下 其实这个边界 已经在下面的if中处理掉了
for(int i=len-; i>=; i--)
{
for(int j=i+; j<=len-; j++)
{
for(int k=i; k<=j; k++)
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+][j]);
if(((a[i]=='(')&&(a[j]==')'))||((a[i]=='[')&&(a[j]==']')))
dp[i][j]=max(dp[i][j],dp[i+][j-]+);
}
}
cout<<dp[][len-]<<endl;;
}
return ;
}

poj 2955 括号匹配 区间dp的更多相关文章

  1. poj 2955 Brackets 括号匹配 区间dp

    题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+ ...

  2. POJ 2955 括号匹配,区间DP

    题意:给你一些括号,问匹配规则成立的括号的个数. 思路:这题lrj的黑书上有,不过他求的是添加最少的括号数,是的这些括号的匹配全部成立. 我想了下,其实这两个问题是一样的,我们可以先求出括号要匹配的最 ...

  3. poj2955括号匹配 区间DP

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5424   Accepted: 2909 Descript ...

  4. 括号匹配 区间DP (经典)

    描述给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来 ...

  5. POJ - 2955 Brackets (区间DP)

    题目: 给出一个有括号的字符串,问这个字符串中能匹配的最长的子串的长度. 思路: 区间DP,首先枚举区间长度,然后在每一个长度中通过枚举这个区间的分割点来更新这个区间的最优解.还是做的少. 代码: / ...

  6. POJ 2955 Brackets(区间DP)题解

    题意:问最多有几个括号匹配 思路:用dp[i][j]表示i到j最多匹配,若i和j构成匹配,那么dp[i][j] = dp[i + 1][j - 1] + 2,剩下情况dp[i][j] = max(dp ...

  7. Poj 2955 brackets(区间dp)

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

  8. POJ 2955 Brackets (区间DP,常规)

    题意: 给出一个字符串,其中仅仅含 “ ( ) [ ] ” 这4钟符号,问最长的合法符号序列有多长?(必须合法的配对,不能混搭) 思路: 区间DP的常规问题吧,还是枚举区间[i->j]再枚举其中 ...

  9. poj 2955 Brackets 【 区间dp 】

    话说这题自己折腾好久还是没有推出转移的公式来啊------------------ 只想出了dp[i][j]表示i到j的最大括号匹配的数目--ค(TㅅT)------------------- 后来搜 ...

随机推荐

  1. linux 线程操作问题undefined reference to 'pthread_create'的解决办法(cmake)

    问题原因: pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a. 所以在使用pthread_create()创建线程时,需要链接该库. 1. 终端:问题解 ...

  2. Program B--CodeForces 492B

    Description Vanya walks late at night along a straight street of length l, lit by n lanterns. Consid ...

  3. 创建和运行shell脚本程序

    转载请标明http://www.cnblogs.com/winifred-tang94/ 要创建一个shell脚本程序,首先新建一个文本文件,然后在这个文本文件中按照shell编程规则输入shell命 ...

  4. greenDao 3.0基础

    引入greenDao3.0 首先在project的gradle文件中引入greenDAO插件 dependencies {       classpath 'com.android.tools.bui ...

  5. <矩阵的基本操作:矩阵相加,矩阵相乘,矩阵转置>

    //矩阵的基本操作:矩阵相加,矩阵相乘,矩阵转置 #include<stdio.h> #include<stdlib.h> #define M 2 #define N 3 #d ...

  6. 06-图1 List Components

    这题主要涉及到了队列,无向图的邻接矩阵表示,图的深度和广度优先搜索.又是糙哥,参考了他的程序(http://www.cnblogs.com/liangchao/p/4288807.html),主要是B ...

  7. 从问题看本质:socket到底是什么(问答式)? .

    转自:http://blog.csdn.net/yeyuangen/article/details/6799575 一.问题的引入——socket的引入是为了解决不同计算机间进程间通信的问题 1.so ...

  8. How to Write Doc Comments for the Javadoc Tool

    http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html This document describe ...

  9. 深入学习:如何实现不同Android设备之间相同应用程序的网络服务发现功能

    在我们的app中添加网络服务发现功能(NSD)以方便在不同的设备上响应局域网中的请求.这种功能对于多设备之间点对点服务来说很有用,例如多人游戏,多人通话,文件共享等. 一,在网络中注册你的服务 注意: ...

  10. A​n​d​r​o​i​d​ ​B​l​u​e​t​o​o​t​h​详​解(Android英文文档相关译文)

    一.Bluetooth Android平台包含了对Bluetooth协议栈的支持,允许机器通过Bluetooth设备进行无线数据交换.应用框架通过Android Bluetooth API访问Blue ...