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. imx6 读取CPU温度

    imx6 读取CPU温度 cat /sys/class/thermal/thermal_zone0/temp Tony Liu 2017-2-11, Shenzhen

  2. e551. 精简的Applet

    Every applet must subclass Applet. import java.applet.*; import java.awt.*; public class BasicApplet ...

  3. 10 个很有用的高级 Git 命令(转)

    英文原文:10 Useful Advanced Git Commands 迄今,我已经使用Git很长一段时间了,考虑分享一些不管你是团队开发还是个人项目,都受用的高级git命令. 1. 输出最后一次提 ...

  4. vnc 多用户登录

    1, 创建新用户: $ useradd tom $ passwd tom 2,  登录到tom账户,创建vnc实例: $ su tom$ vncserver 这时可以看看~/.vnc/目录下,有一些如 ...

  5. mysql 5.7 详细图文安装教程

    官网下载太慢,可以上下载频道下载(mysql-installer-community-5.7.13.0.msi): http://download.csdn.net/download/tan3739/ ...

  6. Linux内核剖析 之 内存管理

    1. 内存管理区 为什么分成不同的内存管理区? ISA总线的DMA处理器有严格的限制:仅仅能对物理内存前16M寻址. 内核线性地址空间仅仅有1G,CPU不能直接訪问全部的物理内存. ZONE_DMA  ...

  7. 哈希----字符串----time33

    //此处只是获得了字符串的hash值,但是该如何散列到hash表中呢?哪个算法会好些?! 1 //在处理以字符串为键值的哈希时,times33哈希算法有着极快的计算效率和很好的哈希分布 //小写英文单 ...

  8. maven 配置多模块项目 pom modules

    所有用Maven管理的真实的项目都应该是分模块的,每个模块都对应着一个pom.xml.它们之间通过继承和聚合(也称作多模块,multi-module)相互关联.那么,为什么要这么做呢?我们明明在开发一 ...

  9. Java课后简答题

    1.简述Java的特点. 面向对象.跨平台性.健壮性.安全性.可移植性.多线程性.动态性等. 2.简述JRE与JDK的区别. JRE(Java Runtime Environment,Java运行时环 ...

  10. GIS-009-Cesium 使用

    //加载ArcGIS 发布的地图服务MapServervar url='http://Jason:6080/arcgis/rest/services/SampleWorldCities/MapServ ...