Brackets

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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=;
char s[N];
int f[N][N];
inline bool check(int i,int j){
if(s[i]=='['&&s[j]==']') return ;
if(s[i]=='('&&s[j]==')') return ;
return ;
}
int main(){
while(~scanf("%s",s+)){
if(s[]=='e') break;
memset(f,,sizeof(f));
int n=strlen(s+);
for(int i=n;i>=;i--)
for(int j=i+;j<=n;j++){
if(check(i,j)) f[i][j]=f[i+][j-]+;
for(int k=i;k<=j;k++) f[i][j]=max(f[i][j],f[i][k]+f[k][j]);
}
printf("%d\n",f[][n]);
}
}

POJ - 2955 Brackets括号匹配(区间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 Brackets(括号匹配一)

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

  3. poj 2955 括号匹配 区间dp

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

  4. poj2955括号匹配 区间DP

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

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

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

  6. POJ 1141 Brackets Sequence (区间DP)

    Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...

  7. UVA 1626 Brackets sequence(括号匹配 + 区间DP)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105116#problem/E 题意:添加最少的括号,让每个括号都能匹配并输出 分析:dp ...

  8. [poj2955/nyoj15]括号匹配(区间dp)

    解题关键:了解转移方程即可. 转移方程:$dp[l][r] = dp[l + 1][r - 1] + 2$ 若该区间左右端点成功匹配.然后对区间内的子区间取max即可. nyoj15:求需要添加的最少 ...

  9. poj 1141 Brackets Sequence(区间DP)

    题目:http://poj.org/problem?id=1141 转载:http://blog.csdn.net/lijiecsu/article/details/7589877 定义合法的括号序列 ...

随机推荐

  1. PCA tries to preserve linear structure, MDS tries to preserve global geometry, and t-SNE tries to preserve topology (neighborhood structure)

    https://colah.github.io/posts/2014-10-Visualizing-MNIST/

  2. php 生成bing词典能导入的xml(有道词典->bing词典)

    编程以来一直用网易有道词典查单词.翻译:最近一直在看英文方面的资料,于是越来越对有道词典(划词.广告,本来想转灵格斯的,但灵格斯没有android版)不满意,一番试用后决定转bing词典,于是想把有道 ...

  3. Java for LeetCode 137 Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  4. 顽石系列:Java技术面试

    顽石系列:Java技术面试 JDBC相关 1.Statement与PreparedStatement的区 别,什什么是SQL注⼊入,如何防⽌止SQL注⼊? PreparedStatement支持动态设 ...

  5. gradlew tasks

    D:\AndroidWorkSpace\Qi\LocalM>gradlew tasks > Configure project : AAAA > Configure project ...

  6. 【Leetcode-easy】Remove Duplicates from Sorted Array

    题目要求:删除排好序的含有重复元素的数组.返回去除重复后的数组长度,并更新原始数组.不能使用额外空间. 思路:要不额外的使用内存空间,那么只有遍历数组,count保持下一个不重复的数字,遍历过程中如果 ...

  7. Spring Boot2.0之多环境配置

    本地开发环境 测试环境 实际项目中 区分不同的环境配置文件信息 首先创建三种不同场景下的配置文件: 内容分别是: ###dev http_url="dev" ###prdhttp_ ...

  8. html5--2.9新的布局元素(5)-hgroup/address

    html5--2.9新的布局元素(5)-hgroup/address 学习要点 了解hgroup/address元素的语义和用法 通过实例理解hgroup/address元素的用法 对照新元素布局与d ...

  9. 在node.js中建立你的第一个HTTp服务器

    这一章节我们将从初学者的角度介绍如何建立一个简单的node.js HTTP 服务器 创建myFirstHTTPServer.js //Lets require/import the HTTP modu ...

  10. centos7搭建redis主从复制,并模拟故障切换。

    Cntos7搭建redis主从复制,并模拟故障主从切换 主从复制搭建 主机:192.168.161.179 从机:192.168.161.180 1.        安装主redis 自己本地环境,关 ...