POJ 2955 Brackets (区间DP)
题目链接:http://poj.org/problem?id=2955
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1977 | Accepted: 1012 |
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
Source
//============================================================================ // Name : POJ.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> using namespace std; const int MAXN=110; char str[MAXN]; int dp[MAXN][MAXN]; int solve(int i,int j) {if(dp[i][j]!=-1)return dp[i][j];if(j<=i)return dp[i][j]=0;else if(j==i+1){if( (str[i]=='('&&str[j]==')')||(str[i]=='['&&str[j]==']') )return dp[i][j]=2;else return dp[i][j]=0;}dp[i][j]=solve(i+1,j);for(int k=i+1;k<=j;k++)if( (str[i]=='('&&str[k]==')')||(str[i]=='['&&str[k]==']') )dp[i][j]=max(dp[i][j],2+solve(i+1,k-1)+solve(k+1,j));return dp[i][j]; }int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout);while(scanf("%s",str)==1){if(strcmp(str,"end")==0)break;memset(dp,-1,sizeof(dp));int n=strlen(str);printf("%d\n",solve(0,n-1));}return 0; }
POJ 2955 Brackets (区间DP)相关推荐
- 【DP】 POJ 2955 Brackets 区间DP
从小区间到大大区间 并计算 当前能否 括号匹配 #include <cstdio> #include <cstring> #include <cstdlib> #i ...
- Brackets POJ - 2955 (区间DP+最大括号匹配子序列)
传送门 题目:给一个长度n(<=100)的只包含'[',']','(',)'的字符串,求最长的完全匹配的子序列.输出长度 题解:区间dp,dp[i][j]表示i~j的最长匹配数,一支dp[i][ ...
- Codeforces 508E Arthur and Brackets 区间dp
Arthur and Brackets 区间dp, dp[ i ][ j ]表示第 i 个括号到第 j 个括号之间的所有括号能不能形成一个合法方案. 然后dp就完事了. #include<bit ...
- POJ - 2955 Brackets (区间DP)
题目: 给出一个有括号的字符串,问这个字符串中能匹配的最长的子串的长度. 思路: 区间DP,首先枚举区间长度,然后在每一个长度中通过枚举这个区间的分割点来更新这个区间的最优解.还是做的少. 代码: / ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
- poj 3280(简单区间dp)
题意:将一个字符串转换成回文串的最小花费. 解题思路:简单的区间dp,dp[i][j]表示从i到j的字符串转换成回文串的最小化费. #include<iostream> #include& ...
- POJ 2955 Brackets(括号匹配一)
题目链接:http://poj.org/problem?id=2955 题目大意:给你一串字符串,求最大的括号匹配数. 解题思路: 设dp[i][j]是[i,j]的最大括号匹配对数. 则得到状态转移方 ...
- 【DP_区间DP专辑】
区间DP是一类在区间上进行动态规划的最优问题,一般是根据问题设出一个表示状态的dp,可以是二维的也可以是三维的,一般情况下为二维.然后将问题划分成两个子问题,也就是一段区间分成左右两个区间,然后将左右 ...
- poj 2955 区间dp
题意:给你一串()[]括号,要你求出这串括号的最大匹配个数.如'('与')'匹配,为2个,'['与']'匹配,为2个 思路:区间dp 状态方程: if(s[i]=='('&&s[j]= ...
最新文章
- 网络工程与机房等精华指引贴
- 工业互联网企业如何做,才能“赋能万物,连接未来”?
- Minor【 PHP框架】1.简介
- Windows Mobile 编程 (Win32) - 获取设备能力
- c语言printf里的自增,笔试题记录:C语言——函数printf()的执行机制;先自增与后自增的区别;取值运算与自增运算的优先级...
- springboot事物注解不生效_springboot事务不生效的几种解决方案
- [FZYZOJ 1002] 雨天
- Vue项目npm run dev后 报错Cannot GET /
- 功能1 -- 顶部导航栏和返回顶部效果
- RhinoMarine v4.0.3 plugin for Rhinoceros 船艇设计分析
- HTML小游戏7 —— 《罗斯魔影》魔法消除游戏(附完整源码)
- 是指因计算机网络不安全导致的风险,网络安全知识竞赛题库
- latex公式及表格识别
- 小米5-root-记录
- lm做自相关做几阶_lm检验(lm检验怎么判断是几阶自相关)
- 用泰勒公式编写一个sin函数--C语言
- python办公笔记分享
- request.setAttribute理解
- 修复Microsoft Office的Word、Excel、PPT丢失图标
- springcloud日志收集框架_从零开始入门K8S| 从Spring Cloud到Kubernetes的微服务迁移实践...