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. 详解centos用户&组权限&添加删除用户

    1.Linux用户操作系统 Linux操作系统是多用户多任务操作系统,包括用户账户和组账户两种: 细分用户账户(普通用户账户,超级用户账户)除了用户账户以为还有组账户所谓组账户就是用户账户的集合,ce ...

  2. BCP批量导入数据时候如何处理表中自动增加的字段

    大容量导入数据时保留标识值 (SQL Server) http://msdn.microsoft.com/zh-cn/library/ms186335(v=sql.120).aspx 使用格式化文件跳 ...

  3. namenode 和datanode无法启动,错误:FSNamesystem initialization failed. datanode.DataNode: Incompatible namespaceIDs

    问题一: namenode无法启动,查看日志,错误信息如下: org.apache.hadoop.hdfs.server.namenode.FSNamesystem: FSNamesystem ini ...

  4. JS教程:词法作用域和闭包 (网络资源)

    varclassA = function(){ ; } classA.prototype.func1 = function(){ var that = this, ; function a(){ re ...

  5. table td的宽度详解

    前言:一直总觉得td的宽度好难驾驭,但万事万物总是有规律的.就像亮剑说的:不用因为怕八路就敬而远之,应该靠上去,熟悉他们,了解他们.   正文:           Table只有Table的宽度是可 ...

  6. poj1080 dp

    //Accepted 200 KB 0 ms //dp //dp[i][j]表示s1用前i个,s2用前j个字符能得到的最大分数 //dp[i][j]=max(dp[i-1][j]+score[s1[i ...

  7. 高效的iOS宏定义

    iOS开发过程中使用一些常用的宏可以提高开发效率,提高代码的重用性:将这些宏放到一个头文件里然后再放到工程中的-Prefix.pch文件中(或者直接放到-Prefix.pch中)直接可以使用,灰常方便 ...

  8. 学会使用Ogitor

    这几天在用Ogre读取Ogitor的场景,遇到了不少问题,在网上也找不到详细的说明,虽然读取Ogitor的场景对很多人来说太简单了,但对一些新手来说就有点难了,我刚开始就觉得是无从下手,因此简单的描述 ...

  9. Emacs+highlight-parentheses高亮括号

    EmacsWiki上关于它的介绍HighlightParentheses,下载最新版请通过作者的GitHub:https://github.com/nschum/highlight-parenthes ...

  10. yii2 数据验证

    控制器层 <?php namespace frontend\controllers; use Yii; use frontend\models\FormsModel; use yii\web\U ...