(区间dp 或 记忆化搜素 )Brackets -- POJ -- 2955
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 i1, i2, …, im where 1 ≤ i1 < i2 < … < im ≤ n, ai1ai2 … 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的更多相关文章
- poj1179 区间dp(记忆化搜索写法)有巨坑!
http://poj.org/problem?id=1179 Description Polygon is a game for one player that starts on a polygon ...
- 【CF607B】Zuma——区间dp(记忆化搜索/递推)
以下是从中文翻译成人话的题面: 给定一个长度小于等于500的序列,每个数字代表一个颜色,每次可以消掉一个回文串,问最多消几次可以消完? (7.16) 这个题从洛谷pend回来以后显示有103个测试点( ...
- P1040 加分二叉树(树上记忆化搜素)
这道题很水 但我没做出来……………………………… 我写的时候状态设计错了,设计dp[l][m][r]为从l到r以m为根的值 这样写遍历状态就是n^3的,会TLE. 而且写路径的时候是用结构体写的,这样 ...
- HDU 4597 Play Game(区间DP(记忆化搜索))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 题目大意: 有两行卡片,每个卡片都有各自的权值. 两个人轮流取卡片,每次只能从任一行的左端或右端 ...
- UVA 10891 Game of Sum(区间DP(记忆化搜索))
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- POJ 1191 棋盘分割 (区间DP,记忆化搜索)
题面 思路:分析公式,我们可以发现平均值那一项和我们怎么分的具体方案无关,影响答案的是每个矩阵的矩阵和的平方,由于数据很小,我们可以预处理出每个矩阵的和的平方,执行状态转移. 设dp[l1][r1][ ...
- UVA1351-----String Compression-----区间DP(记忆化搜索实现)
本文出自:http://blog.csdn.net/dr5459 题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&a ...
- 二进制数(dp,记忆化搜索)
二进制数(dp,记忆化搜索) 给定k个<=1e6的正整数x(k不大于10),问最小的,能被x整除且只由01组成的数. 首先,dp很好写.用\(f[i][j]\)表示i位01串,模ki的值是j的数 ...
- 蓝桥杯历届试题 地宫取宝 dp or 记忆化搜索
问题描述 X 国王有一个地宫宝库.是 n x m 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被带到地宫的入口,国王要求他只能向右或向下行走. 走 ...
随机推荐
- Linux apt-get命令
一.简介 Ubuntu系列系统包管理工具. 二.常用指令 1.查询功能 apt-cache search package 搜索软件包 apt-cache show package 获取包的相关 ...
- Liunx cal
1.命令格式: cal [参数][月份][年份] 2.命令功能: 用于查看日历等时间信息,如只有一个参数,则表示年份(1-9999),如有两个参数,则表示月份和年份 3.命令参数: -1 显示一个月的 ...
- win7系统 无线上网卡 共享网络,设置成wifi热点
给家人买了一个新的智能手机,用的移动神州行套餐,没有开通3G,想更新一些应用软件,于是想到能不能用电脑上的无线上网卡. 在网上找到了一方法,试了一下,还真是可以. 步骤如下: 用无线上网卡拨号上网,并 ...
- js 闭包 理解 copy
闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 下面就是我的学习笔记,对于Javascript初学者应该是很有用的. 一.变量的作用域 要理解 ...
- linux下svn导入新目录到svn服务器特定地址
svn import transplant-apps/ svn://xx.xx.xx.90/ -m "changelog:add transplant-apps to 90-svn" ...
- POJ3254或洛谷1879 Corn Fields
一道状压\(DP\) POJ原题链接 洛谷原题链接 很显然的状压,\(1\)表示种植,\(0\)表示荒废. 将输入直接进行状压,而要满足分配的草场是适合种草的土地,即是分配时的状态中的\(1\),在输 ...
- 放大Button热区的方法哟
//添加图片不能用backgroundimage [btn setImage:image5 forState:]; //然后 btn.imageEdgeInsets = UIEdgeInsetsMak ...
- UI设计教程:如何在设计中运用颜色
灰度优先 我们习惯在设计阶段的早期就开始调整颜色和色调.但是,当你意识到自己花了3个小时来调整主色调的时候,你发现这种行为毫无帮助.虽然把玩颜色很有吸引力,但是你应该避免在设计初期进行这种行为. 相反 ...
- linux内socket服务器无法连接windows
今天在试socket的时候出现了一个问题:问题概述是这样的: 1.linux采用centOS7(mini)版本,虚拟机版本VMware12,网卡设置NAT 2.服务器和客户端都在windows上,通讯 ...
- maven 介绍(zz )
Maven 编辑 目录 1简介 2特点 3常用命令 4推荐书籍 5Win7配置 6生命周期 1 1简介 Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构 ...