http://poj.org/problem?id=2955

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

p[i][j]表示从i到j个可以组成的括号最大值,则若dp[i+1][j]已取到最大值,则dp[i][j] 的取值为 dp[i+1][j] , 或若 s[i] 与 第i+1个到第j个中某个括号匹配(假定为第k个),则有dp[i][j] = max(dp[i+1][j], dp[i+1][k-1] + 2 + dp[k+1][j]) (注:要考虑k == i+1的情况要分开讨论)

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std; const int INF = 0x3f3f3f3f;
#define N 105 char s[N];
int dp[N][N]; int main()
{
while(scanf("%s", s), strcmp(s, "end"))
{
int i, j, k, len=strlen(s)-; memset(dp, , sizeof(dp)); for(i=len-; i>=; i--)
{
for(j=i+; j<=len; j++)
{
dp[i][j] = dp[i+][j]; for(k=i+; k<=j; k++)
{
if((s[i]=='(' && s[k]==')') || (s[i]=='[' && s[k]==']'))
{
if(k==i+) dp[i][j] = max(dp[i][j], dp[k+][j]+);
else dp[i][j] = max(dp[i][j], dp[i+][k-]+dp[k+][j]+);
}
}
}
} printf("%d\n", dp[][len]);
}
return ;
}

记忆化索搜:

(感觉记忆化搜索只是把在递归中已经计算过的值给记录下来, 不知道是否理解有悟,慢慢用吧!!!)

#include<stdio.h>
#include<string.h>
#include<stdlib.h> #define N 105
#define max(a,b) (a>b?a:b) char s[N];
int dp[N][N]; int OK(int L, int R)
{
if((s[L]=='[' && s[R]==']') || (s[L]=='(' && s[R]==')'))
return ;
return ;
} int DFS(int L, int R)
{
int i; if(dp[L][R]!=-)
return dp[L][R];
if(L+==R)
return OK(L,R);
if(L>=R)
return ; dp[L][R] = DFS(L+, R); for(i=L+; i<=R; i++)
{
if(OK(L,i))
dp[L][R] = max(dp[L][R], DFS(L+, i-)+DFS(i+, R)+);
}
return dp[L][R];
} int main()
{
while(scanf("%s", s), strcmp(s, "end"))
{
memset(dp, -, sizeof(dp));
printf("%d\n", DFS(, strlen(s)-));
}
return ;
}

(区间dp 或 记忆化搜素 )Brackets -- POJ -- 2955的更多相关文章

  1. poj1179 区间dp(记忆化搜索写法)有巨坑!

    http://poj.org/problem?id=1179 Description Polygon is a game for one player that starts on a polygon ...

  2. 【CF607B】Zuma——区间dp(记忆化搜索/递推)

    以下是从中文翻译成人话的题面: 给定一个长度小于等于500的序列,每个数字代表一个颜色,每次可以消掉一个回文串,问最多消几次可以消完? (7.16) 这个题从洛谷pend回来以后显示有103个测试点( ...

  3. P1040 加分二叉树(树上记忆化搜素)

    这道题很水 但我没做出来……………………………… 我写的时候状态设计错了,设计dp[l][m][r]为从l到r以m为根的值 这样写遍历状态就是n^3的,会TLE. 而且写路径的时候是用结构体写的,这样 ...

  4. HDU 4597 Play Game(区间DP(记忆化搜索))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 题目大意: 有两行卡片,每个卡片都有各自的权值. 两个人轮流取卡片,每次只能从任一行的左端或右端 ...

  5. UVA 10891 Game of Sum(区间DP(记忆化搜索))

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. POJ 1191 棋盘分割 (区间DP,记忆化搜索)

    题面 思路:分析公式,我们可以发现平均值那一项和我们怎么分的具体方案无关,影响答案的是每个矩阵的矩阵和的平方,由于数据很小,我们可以预处理出每个矩阵的和的平方,执行状态转移. 设dp[l1][r1][ ...

  7. UVA1351-----String Compression-----区间DP(记忆化搜索实现)

    本文出自:http://blog.csdn.net/dr5459 题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&a ...

  8. 二进制数(dp,记忆化搜索)

    二进制数(dp,记忆化搜索) 给定k个<=1e6的正整数x(k不大于10),问最小的,能被x整除且只由01组成的数. 首先,dp很好写.用\(f[i][j]\)表示i位01串,模ki的值是j的数 ...

  9. 蓝桥杯历届试题 地宫取宝 dp or 记忆化搜索

    问题描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被带到地宫的入口,国王要求他只能向右或向下行走. 走 ...

随机推荐

  1. Linux操作系统-系统安装与分区

    .磁盘分区 使用分区工具在磁盘上划分几个逻辑部分,一旦分成几个分区,不同类型的目录和文件可以存储进不同的分区2.分区类型主分区:最多只能有4个扩展分区:最多只能有1个:主分区加扩展分区最多有4个:扩展 ...

  2. python添加fluent日志记录

    istio默认会进行日志的记录,但是仅仅记录到服务.以及服务之间调用的信息,不记录业务日志. 如: 所以需要添加业务日志记录. 1.python引入package fluentmsgpack 2.代码 ...

  3. 初识restful api接口

    一.restful api接口举例 实现功能 传统方式 restful方式 url HTTP方法 url HTTP方法 查询 /user/query?name=knyel GET /user?name ...

  4. bloomfilter 以及count min sketch

    bloomfilter http://blog.csdn.net/v_july_v/article/details/6685894 count min sketch http://www.cnblog ...

  5. Windows 下 Phpstrom 配置git使用

    首先先去下载 git 下载链接 https://git-scm.com/download/winphpstrom 配置git    链接  http://jingyan.baidu.com/artic ...

  6. Centos查公网IP地址

    [root@syy ~]# curl icanhazip.com 115.29.208.111

  7. About the Cron Expression

    About the Cron Expression Cron is use in Linux for the time schedule Format Seconds Minutes Hours Da ...

  8. SparkStreaming 监控文件目录

    SparkStream 监控文件目录时,只能监控文件内是否添加新的文件,如果文件名没有改变只是文件内容改变,那么不会检测出有文件进行了添加. )) )).reduceByKey(_ + _) word ...

  9. Scrapy框架爬虫

    一.sprapy爬虫框架 pip install pypiwin32 1) 创建爬虫框架 scrapy startproject Project # 创建爬虫项目 You can start your ...

  10. TOMCAT内存溢出及大小调整的实现方法

    一.tomcat内存设置问题 收藏 在使用Java程序从数据库中查询大量的数据或是应用服务器(如tomcat.jboss,weblogic)加载jar包时会出现java.lang.OutOfMemor ...