Brackets

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8716   Accepted: 4660

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

分析

dp[l][r]表示区间[l,r]的答案。

状态转移方程,详见代码

  • dp[i][j] = max(dp[i+1][j],dp[i][j-1]);
  • if ((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
      dp[i][j]=dp[i+1][j-1]+2;
  • dp[l][r]=max(dp[l][r],dp[l][k]+dp[k+1][r]);

code

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[];
int dp[][]; int main()
{
while (scanf("%s",s)!=EOF)
{
memset(dp,,sizeof(dp));
if (s[]=='e') break;
int len = strlen(s);
for (int i=len-; i>=; --i)
{
for (int j=i; j<len; ++j)
{
dp[i][j] = max(dp[i+][j],dp[i][j-]);
if ((s[i]=='('&&s[j]==')')||(s[i]=='['&&s[j]==']'))
dp[i][j]=dp[i+][j-]+;
for (int k=i; k<j; ++k)
dp[i][j] = max(dp[i][j],dp[i][k]+dp[k+][j]);
}
}
printf("%d\n",dp[][len-]);
}
return ;
}

poj2955:Brackets的更多相关文章

  1. POJ2955 Brackets —— 区间DP

    题目链接:https://vjudge.net/problem/POJ-2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  2. POJ-2955 Brackets(括号匹配问题)

    题目链接:http://poj.org/problem?id=2955 这题要求求出一段括号序列的最大括号匹配数量 规则如下: the empty sequence is a regular brac ...

  3. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  4. 间隔DP基础 POJ2955——Brackets

    取血怒.first blood,第一区间DP,这样第一次没有以某种方式在不知不觉中下降~~~ 题目尽管是鸟语.但还是非常赤裸裸的告诉我们要求最大的括号匹配数.DP走起~ dp[i][j]表示区间[i, ...

  5. POJ2955 Brackets(区间DP)

    给一个括号序列,求有几个括号是匹配的. dp[i][j]表示序列[i,j]的匹配数 dp[i][j]=dp[i+1][j-1]+2(括号i和括号j匹配) dp[i][j]=max(dp[i][k]+d ...

  6. POJ2955 Brackets (区间DP)

    很好的区间DP题. 需要注意第一种情况不管是否匹配,都要枚举k来更新答案,比如: "()()()":dp[0][5]=dp[1][4]+2=4,枚举k,k=1时,dp[0][1]+ ...

  7. 各种DP总结

    一.数位DP 1.含有或不含某个数“xx”: HDU3555 Bomb HDU2089 不要62 2.满足某些条件,如能整除某个数,或者数位上保持某种特性: HDU3652 B-number Code ...

  8. [总结-动态规划]经典DP状态设定和转移方程

    马上区域赛,发现DP太弱,赶紧复习补上. #普通DP CodeForces-546D Soldier and Number Game 筛法+动态规划 待补 UVALive-8078 Bracket S ...

  9. POJ2955:Brackets(区间DP)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

随机推荐

  1. ElasticSearch 处理自然语言流程

    ES处理人类语言 ElasticSearch提供了很多的语言分析器,这些分析器承担以下四种角色: 文本拆分为单词 The quick brown foxes → [ The, quick, brown ...

  2. 面向对象(OOP)二

    一.“魔术”函数 - 自动调用 魔术方法 在面向对象有一些特别的方法,无需特别定义,已自动具备某些功能,例如构造函数__construt,这些方法统称魔术方法,在日后的编程中,可以使用这些方法的特性设 ...

  3. codevs 1664 清凉冷水

    1664 清凉冷水 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold         题目描述 Description 闷热的夏天,威斯康辛州的奶制品地区提供冷水供奶牛 ...

  4. The eighth day

    time n(名词):时间:次,时代,时刻: vt(及物动词):为...安排时间:测定...的时间:调准(机械的速度): vi(不及物动词):合拍,和谐,打拍子 files (原型是fly) vi(不 ...

  5. 设置mapcontrol的鼠标样式

    http://blog.itpub.net/14999074/viewspace-586515/ mapcontrol的鼠标样式 this.axMapControl1.MousePointer=esr ...

  6. c语言数据结构:用标志位实现循环队列

    #include<stdio.h> #include<stdlib.h> #define MAXSIZE 10//定义队列长度 ;//定义标志位 typedef struct ...

  7. 基于PowerShell的Lync Server管理 使用C# 之 Telephony 功能 查看 /修改

    本以为这个属性可以在用户信息中直接反应出来,但是看了好几遍还是没找到这个属性名称 这个功能没有在get-User 的结果中直接反映出来 但是可以通过 Property 查找单个选项 如: Get-Cs ...

  8. hdu-2256 Problem of Precision---矩阵快速幂+数学技巧

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2256 题目大意: 题目要求的是(sqrt(2)+sqrt(3))^2n %1024向下取整的值 解题 ...

  9. latex-word

    http://blog.sina.com.cn/s/blog_565e747c0100qxma.html 附:PowerPoint 中插入LaTeX公式的插件,IguanaTex,功能和TeXsWor ...

  10. tomcat的启动和部署

    方法/步骤 1 一.安装JDK和Tomcat 1,安装JDK:直接运行jdk-7-windows-i586.exe可执行程序,默认安装即可. 备注:路径可以其他盘符,不建议路径包含中文名及特殊符号. ...