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. C++函数的高级特性

    对比于 C 语言的函数,C++增加了重载(overloaded).内联(inline).const 和 virtual 四种新机制.其中重载和内联机制既可用于全局函数也可用于类的成员函数,const ...

  2. 使用GitHub和Eclipse进行javaEE开发步骤

    下载Git客户端:链接:http://pan.baidu.com/s/1jIueUEy 密码:7gef; 下载Eclipse javaee客户端:http://www.eclipse.org/down ...

  3. 很好的hadoop学习博客实际操作训练(旧版本)

    实际操作 http://www.cnblogs.com/xia520pi/archive/2012/05/16/2504205.html 流程解析 http://www.cnblogs.com/spo ...

  4. android获取手机屏幕分辨率

    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceSt ...

  5. NGUI与EasyTouch结合使用

    用了EasyTouch插件一段时间了,小问题还是有一些,总体来说用起来还行.这篇文章我就来说说EasyTouch和NGUI的结合. 总体来说触摸屏幕也就三种情况. 1.触摸事件只响应NGUI部分,不响 ...

  6. ubuntu下使用sublime text进行C编程开发尝鲜

    1 选择编译系统 2 编写文件,编译(Ctrl+B)运行(Shift+Ctrl+B)

  7. 查看当前mysql数据库实例中,支持的字符集有哪些,或者是否支持某个特定字符集

    需求描述: 查看当前mysql实例中支持哪些字符集,过滤特定的字符集 操作过程: 1.通过show character set来进行查看 mysql> show character set; + ...

  8. Redis(二)-- 发布订阅、事务、安全、持久化

    一.Redis发布订阅 Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. 打开两个窗口:session1 和 session2 在sess ...

  9. Python 处理命令行参数

    optparse模块用于从命令行直接读取参数,用法基本与 argparse模块 一致,如下: #!/usr/bin/env python #-*- coding:utf-8 -*- from optp ...

  10. 安卓教程:提取APK程序里图片资源的方法

    有些APK程序里的图标.图片很漂亮,在使用程序时你可能会想,如果能把这些漂亮的图标.图片提取出来就好了,其实这是可以办到的,请看教程. 本教程以“电影超人”的APK安装包为例,其它APK程序的提取方法 ...