(区间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 个格子的矩阵.每个格子放一件宝贝.每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被带到地宫的入口,国王要求他只能向右或向下行走. 走 ...
随机推荐
- js 正则表达式:密码必须由6-12位数字加字母组成
^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$
- input,textarea在ios和Android上阴影和边框的处理方法(在移动端)
1.去掉ios上阴影的方法只需要在css文件上添加input,textarea{-webkit-appearance: none;}就可以了 2.在移动端上input和textarea边框问题,也是在 ...
- React-router4 第四篇 Custom Link 自定义链接
直接贴代码 虽说我这么懒的人应该不会自定义标签,何必呢,,但是我还是看了官方的例子 直接抄过来, exact 属性:根据我的测试,这个属性应该和路由的精确匹配有关有关,当值为true时,路由是会精确匹 ...
- 搭建FTP服务器 window7
1.安装IIS组件,打开控制面板-->程序和功能,点击打开或关闭windows功能 找到Internet信息服务,勾选FTP服务器和Web管理工具下的IIS管理控制台进行安装ftp,如图所示 2 ...
- 解决loadrunner录制页面的乱码问题
以下亲自验证了的:好用. 三步解决loadrunner录制页面的乱码问题 第一步:去lr 的vugen的Tools -> Recoding Options -> Advanced ...
- Python.tornado.2.tornado.options
记录Tornado-4.0.2源码的阅读,学习,分析 options.py 1. imports 部分 1.1 __future__ from __future__ import absolute_i ...
- 数据存储(直接写入、NSUserDefaults、NSkeyedArchiver)
ios中常用文件存取的方法有: 1.直接写文件的方式,可以存储的对象有NSString.NSArray.NSDictionary.NSData.NSNumber,数据全部存放在一个属性列表文件(*.p ...
- LocalStorage的一些使用
LocalStorage是什么 LocalStorage 是在Html5中出现的一种本地存储.说到本地存储,大家立马会联想到Cookie,还有SqlLite. LocalStorage 中的数据不会像 ...
- [Robot Framework] 通过SikuliLibrary可以获取到图片,但是点击失效
执行时,可以看到鼠标已经移动到图片上了,但是点击失效,日志也没有报错 后来发现是windows权限的问题. 通过打开Control Panel->System and Security-> ...
- Spring PropertyResolver 占位符解析(二)源码分析
Spring PropertyResolver 占位符解析(二)源码分析 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) ...