Brackets

My Tags (Edit)

Source : Stanford ACM Programming Contest 2004

Time limit : 1 sec Memory limit : 32 M

Submitted : 188, Accepted : 113

5.1 Description

We give the following inductive definition of a “regular brackets” sequence:

• the empty sequence is a regular brackets sequence,

• if s is a regularbrackets 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 ≤ n, ai1ai2 …aim is a regular brackets sequence.

5.2 Example

Given the initial sequence ([([]])], the longest regular brackets subsequence is [([])].

5.3 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. For example:

((()))

()()()

([]])

)[)(

([][][)

end

5.4 Output

For each input case, the program should print the length of the longest possible regular brackets subsequence on a single line. For example:

6

6

4

0

6

一道简单的区间DP题目

关于区间DP,可以参照这个博客

http://blog.csdn.net/dacc123/article/details/50885903

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm> using namespace std;
char a[105];
int dp[105][105];
int main()
{
while(scanf("%s",a+1)!=EOF)
{
if(a[1]=='e')
break;
memset(dp,0,sizeof(dp));
int n=strlen(a+1);
for(int len=1;len<n;len++)
{
for(int i=1;i+len<=n;i++)
{
int j=i+len;
if((a[i]=='('&&a[j]==')')||(a[i]=='['&&a[j]==']'))
dp[i][j]=dp[i+1][j-1]+2;
else
dp[i][j]=dp[i+1][j-1];
for(int k=i;k<j;k++)
{
if(dp[i][j]<dp[i][k]+dp[k+1][j])
dp[i][j]=dp[i][k]+dp[k+1][j];
}
}
}
printf("%d\n",dp[1][n]);
}
return 0;
}

HOJ 1936&POJ 2955 Brackets(区间DP)的更多相关文章

  1. poj 2955 Brackets (区间dp基础题)

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

  2. poj 2955"Brackets"(区间DP)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给你一个只由 '(' , ')' , '[' , ']' 组成的字符串s[ ], ...

  3. poj 2955 Brackets (区间dp 括号匹配)

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

  4. POJ 2955 Brackets 区间DP 入门

    dp[i][j]代表i->j区间内最多的合法括号数 if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']') dp[i][j] ...

  5. POJ 2955 Brackets(区间DP)

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

  6. POJ 2955 Brackets 区间DP 最大括号匹配

    http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...

  7. POJ 2995 Brackets 区间DP

    POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...

  8. A - Brackets POJ - 2955 (区间DP模板题)

    题目链接:https://cn.vjudge.net/contest/276243#problem/A 题目大意:给你一个字符串,让你求出字符串的最长匹配子串. 具体思路:三个for循环暴力,对于一个 ...

  9. POJ 2955 Brackets 区间合并

    输出一个串里面能匹配的括号数 状态转移方程: if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']')             dp ...

随机推荐

  1. 74hc165三片级联

    3片74HC165进行级联,用于扩展IO口,读取外界设备的数据. unsigned int read_74165(void) { unsigned ; unsigned ; //三片74hc165,需 ...

  2. : error C3861: “Sleep”: 找不到标识符

    编译的时候:error C3861: “sleep”: 找不到标识符,是什么原因啊?编译的时候:error C3861: “sleep”: 找不到标识符,是什么原因啊?哪位好心的哥哥告诉我吧,分全是你 ...

  3. linux -- ubuntuserver 安装Apache后,修改默认目录和分布式配置文件可执行

    装了apache后,访问出现The requested URL *** was not found on this server,查看了下/etc/httpd/conf/httpd.conf,发现原因 ...

  4. PHP中如何获取网站根目录物理路径

    在php程序开发中经常需要获取当前网站的目录,我们可以通过常量定义获取站点根目录物理路径,方便在程序中使用. 下面介绍几种常用的获取网站根目录的方法. php获取网站根目录方法一: <?php ...

  5. 演示-JQuery属性选择器

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

  6. Unity 大版本更新之APK的下载与覆盖安装

    作为一个游戏开发者,更新这个技能是必不可少的!更新分为游戏内的更新,也就是所谓的资源热更包括AssetBundle更新和代码更新,代码其实也是所谓的二进制文件,在安卓上和普通资源文件毫无差异,然而在I ...

  7. mysql数据库,什么是数据库的全备份?

    需求描述: 今天要做mysql数据库的全备份,那么就要弄清楚一个概念,到底什么是数据库的全备份呢. 概念解释: 数据库的全备份,就是在一个给定的时间点,对于mysql服务器管理的所有的数据进行备份. ...

  8. 虚幻4 - ARPG实战教程(第一季)

    在广受欢迎的的<虚幻4高速开发入门>视频教程之后.我收到了许多的反馈,当中大量的同学想要一个实战类的教程.于是,我花了一段时间准备之后,推出了新的一系列实战教程. 希望以深入浅出的方式.解 ...

  9. 九度 1547 出入栈(递推DP)

    题目描述: 给定一个初始为空的栈,和n个操作组成的操作序列,每个操作只可能是出栈或者入栈.要求在操作序列的执行过程中不会出现非法的操作,即不会在空栈时执行出栈操作,同时保证当操作序列完成后,栈恰好为一 ...

  10. CCNet持续集成编译中SVN问题解决

    SVN问题 BUILD EXCEPTION Error Message: ThoughtWorks.CruiseControl.Core.CruiseControlException: Source ...