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. ArcGis for WPF(1)

    这篇文章主要是讲窗体中怎么加载一张在线地图. 第一步:首先引用ESRI.ArcGIS.Client.dll类库. 第二步:在XAML中添加如下代码: <Window x:Class=" ...

  2. 00_ForTest

    -----该页是爬虫的测试页请忽略------- 1234545@qq.comadasdsdasdsad阿打算多少其热情为 asdasdasdasd4w5wsdvv啊实打实大 啊实打实大asdasda ...

  3. Hdu 4514 湫湫系列故事——设计风景线

    湫湫系列故事--设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total ...

  4. bzoj1688: [Usaco2005 Open]Disease Manangement 疾病管理

    思路:状压dp,枚举疾病的集合,然后判断一下可行性即可. #include<bits/stdc++.h> using namespace std; #define maxs 400000 ...

  5. 【BZOJ2793】【数学】[Poi2012]Vouchers

    Description 考虑正整数集合,现在有n组人依次来取数,假设第i组来了x人,他们每个取的数一定是x的倍数,并且是还剩下的最小的x个. 正整数中有m个数被标成了幸运数,问有哪些人取到了幸运数. ...

  6. erlang 里的if 和 case

    case Expression of Pattern1 [when Guard1] -> Expr_seq1; Pattern2 [when Guard2] -> Expr_seq2; … ...

  7. Log4j 密码屏蔽

    Log4j filter to mask Payment Card numbers (PCI DSS) According to PCI DSS (Payment Card Industry Data ...

  8. Sass中的Map 详解

    Sass中的Map长什么样 Sass 的 map 常常被称为数据地图,也有人称其为数组,因为他总是以 key:value 成对的出现, Sass 的 map 长得与 JSON 极其相似. json: ...

  9. c# winform 隐藏tabcontrol标签

    Apperarance 属性:Faltbuttons SizeMode属性:Fixed 各个TabPage的Text :空 ItemSize : Width=0;Height=1;

  10. JavaFX 开发是遇到一些问题记录

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "PingFang SC"; color: #000000; min-he ...