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. JS如何获取上传标签的文件路径和文件名?

    如何使用JS获取type="file"的标签上传文件的文件路径及文件名: 代码: <!doctype html><html lang="en" ...

  2. 9.17-9.19h5日记

    9.17 ❤关于position(absolute.relative) position:relative: top:100px: right:200px: 如果relative设置值,则是根据它原来 ...

  3. 求助Ubuntu16.10如何设置默认启动为字符界面

    字符界面:sudo systemctl set-default multi-user.target图形界面:sudo systemctl set-default graphical.target

  4. 8P - 钱币兑换问题

    在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input 每行只有一个正整数N,N小于32768. Output 对应每个输入,输出兑换方法数. ...

  5. 前端面试问题css汇总

    1,行内元素有哪些?块级元素有哪些?空元素有哪些?CSS的盒模型? 块级元素:div p h1 h2 h3 h4 form ul li 行内元素: a b br i span input select ...

  6. BZOJ1001或洛谷4001 [BJOI2006]狼抓兔子

    BZOJ原题链接 洛谷原题链接 显然就是求最小割. 而对于一个平面图有结论,最大流=最小割=对偶图最短路. 所以这题可用最大流或是转换为对偶图求最短路,这里我是用的对偶图. 虽然理论上按上界算,这题\ ...

  7. 需求文件requirements.txt的创建及使用

    pip freeze >requirements.txt pip install -r requirements.txt

  8. 清晰易懂!关于PS入门的超详细笔记!

    给大家分享一篇关于PS入门的超详细笔记!原理讲解清晰明了,虽不是新版本解析,但都是新手学习PS必掌懂的一些知识点,灰常的实用,转走收藏学习! 编辑:千锋UI设计 来源:PS学堂

  9. Python之路(第八篇)Python内置函数、zip()、max()、min()

    一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...

  10. java的nio例子

    package main; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.Inet ...