Brackets

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 ≤ 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 思路:动态规划,设dp[i][j]表示从i到j这段子串匹配的最大长度,则状态转移方程分两种情况,1.若从i到j-1这些字符中没有一个能与j匹配,则 dp[i][j] = dp[i][j-1],这是显然的;2.若从i到j-1中有字符能与j匹配(可能不止一个,并设他们组成集合为A),则 dp[i][j] = max(dp[i][j],dp[i][k-1]+dp[k+1][j-1]+2)(k属于集合A),加2是因为一旦匹配成功一次长度就会增加2.
 #include<cstdio>
#include<cstring>
#include<iostream>
#define MAXN 111
using namespace std;
int dp[MAXN][MAXN];
char str[MAXN];
bool OK(int i,int j){return (str[i] == '(' && str[j] ==')') || (str[i] == '[' && str[j] == ']');}
int main(){
//freopen("in.cpp","r",stdin);
while(~scanf("%s",str) && strcmp(str,"end")){
memset(dp,,sizeof(dp));
int len = strlen(str);
for(int i = ;i < len;i ++){
for(int j = i-;j >= ;j--){
dp[j][i] = dp[j][i-];
for(int k = j;k < i;k ++)
if(OK(k,i)) dp[j][i] = max(dp[j][i],dp[j][k-] + dp[k+][i-]+);
}
}
printf("%d\n",dp[][len-]);
memset(str,,sizeof(str));
}
return ;
}
												

POJ -- 2955的更多相关文章

  1. poj 2955 Brackets dp简单题

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

  2. poj 2955 Brackets

    题目链接:http://poj.org/problem?id=2955 思路:括号匹配问题,求出所给序列中最长的可以匹配的长度(中间可以存在不匹配的)例如[(])]有[()]符合条件,长度为4 dp[ ...

  3. POJ 2955 (区间DP)

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

  4. (区间dp 或 记忆化搜素 )Brackets -- POJ -- 2955

    http://poj.org/problem?id=2955 Description We give the following inductive definition of a “regular ...

  5. POJ 2955 Brackets(括号匹配一)

    题目链接:http://poj.org/problem?id=2955 题目大意:给你一串字符串,求最大的括号匹配数. 解题思路: 设dp[i][j]是[i,j]的最大括号匹配对数. 则得到状态转移方 ...

  6. POJ 2955:Brackets(区间DP)

    http://poj.org/problem?id=2955 题意:给出一串字符,求括号匹配的数最多是多少. 思路:区间DP. 对于每个枚举的区间边界,如果两边可以配对成括号,那么dp[i][j] = ...

  7. POJ 2955 Brackets --最大括号匹配,区间DP经典题

    题意:给一段左右小.中括号串,求出这一串中最多有多少匹配的括号. 解法:此问题具有最优子结构,dp[i][j]表示i~j中最多匹配的括号,显然如果i,j是匹配的,那么dp[i][j] = dp[i+1 ...

  8. 区间DP poj 2955

    求最多有几个括号可以匹配 #include<stdio.h> #include<string.h> #include<algorithm> using namesp ...

  9. POJ 2955 Brackets(区间DP)

    题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <vector& ...

  10. poj 2955 括号匹配 区间dp

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

随机推荐

  1. html-----007

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. sublime_text编辑器下载安装使用

    1.sublime_text下载 https://www.sublimetext.com/3   (选择相对应的版本) 2.通过package control安装插件 https://packagec ...

  3. 使用jeesite org.springframework.beans.NotReadablePropertyException: Invalid property 'tfxqCmsAccount.id' of bean class

    一对多 对子表添加时在form表单 path="tfxqCmsAccount.id"页面报错,对比了下其他可行的,发现其自动生成的子类少了个构造方法 加上 public TfxqC ...

  4. 06_WebService与Socket的区别

    [区别] 区别1. Socket是基于TCP/IP的传输层协议. WebService是基于HTTP协议传输数据的,HTTP是基于TCP的应用层协议. 区别2. WebService采用了基于HTTP ...

  5. linux管理网络连接指令

    ethtool eth0  查看 eth0对应网卡的设置 ethtool -i eth0 查看 eth0网卡的驱动信息 ethtool -S eth0 查看网卡的统计信息 ethtool -s eth ...

  6. MySQL事务隔离级别初探

    MySQL有四种隔离级别,分别是: READ UNCOMMITTED(未提交读) READ COMMITTED(提交读) REPEATABLE READ (可重复读) SERIALIZABLE(可串行 ...

  7. POJ 3687 Labeling Balls 逆向建图,拓扑排序

    题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...

  8. c# .net使用SqlDataReader注意的几点

    转自:http://blog.knowsky.com/258608.htm 1.当SqlDataReader没有关闭之前,数据库连接会一直保持open状态,所以在使用SqlDataReader时,使用 ...

  9. NULL, nil, Nil详解

    原文地址:http://blog.csdn.net/wzzvictory/article/details/18413519    感谢原作者 作者:wangzz 原文地址:http://blog.cs ...

  10. swift 语法 - 以及学习资料

    附上一些swift的一下学习资料: 1.Swift语法介绍官方英文版:The Swift Programming Language 2.Swift与Objective-C相互调用Using Swift ...