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. LCD驱动 15-1

    app: read() ---------------------------------------------------------------------------------------- ...

  2. Rhel6-heartbeat+lvs配置文档

    系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119 server19.example.com 192.168.12 ...

  3. Mvc学习--1

    1.缓存机制[OutputCache(Duration=10)] 后面的 duration 表示缓存时间 直接放在action上面 是一个特性2.文件上传 @using (Html.BeginForm ...

  4. js和C#中的编码和解码

    同一个字符串,用URL编码和HTML编码,结果是完全不同的. JS中的URL编码和解码.对 ASCII 字母和数字及以下特殊字符无效: - _ . ! ~ * ' ( ) ,/?:@&=+$# ...

  5. 在shell脚本中进行条件控制以及使用循环

    转载请标明:http://www.cnblogs.com/winifred-tang94/ if条件语句语法: if [ 条件表达式 ] then 代码 else 代码 fi 注意:在上面的if条件语 ...

  6. SharePoint表单和工作流 - Nintex篇(四)

    博客地址 http://blog.csdn.net/foxdave 接上篇点击打开链接 "Manage workflow constants" 管理工作流常量.这里可以管理工作流中 ...

  7. Visual Studio 2013 如何关闭调试而不关闭IIS Express

    在VS主面板打开:工具->选项->调试->编辑继续   取消选中[启用"编辑并继续"] 就OK了 (英文版的请对应相应的操作) 不过这是针对所有的调试,如果你想针 ...

  8. linux命令存放 bash: xxx command not found

    参考资料:http://blog.sina.com.cn/s/blog_688077cf01013qrk.html 提示:bash: xxx command not found 首先就要考虑root ...

  9. hdu2476 区间dp

    //Accepted 300 KB 31 ms //区间dp 思路完全网上看的 #include <cstdio> #include <cstring> #include &l ...

  10. JS 数组去重!!!

    var arr = [1,2,3,1,1,1,3,5,3,6,2]; var newArr=[]; for(var i = 0; i < arr.length-1; i++){ var onOf ...