POJ 2955 区间DP必看的括号匹配问题,经典例题
Brackets
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14226 Accepted: 7476
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
Stanford Local 2004
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
bool match(char a,char b);
#define mst(a,b) memset((a),(b),sizeof(a))
const int maxn=500;
int dp[maxn][maxn];
int main()
{
string ob;
while(cin>>ob)
{
if(ob=="end") break;
mst(dp,0);
for(int len=2;len<=ob.length( );len++){
for(int i=1;i<=ob.length( )+1-len;i++){
int j=len+i-1;
if(match(ob[i-1],ob[j-1])) dp[i][j]=dp[i+1][j-1]+2;
for(int k=i;k<j;k++)
{
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+1][j]);
}
}
}
// for(int len=1;len<ob.length( )-2;len++) cout<<dp[len][len+3]<<' ';
cout<<dp[1][ob.length( )]<<endl;
}
}
bool match(char a,char b)
{
if(a=='('&&b==')') return 1;
if(a=='['&&b==']') return 1;
else return 0;
}
POJ 2955 区间DP必看的括号匹配问题,经典例题的更多相关文章
- POJ 2955 (区间DP)
题目链接: http://poj.org/problem?id=2955 题目大意:括号匹配.对称的括号匹配数量+2.问最大匹配数. 解题思路: 看起来像个区间问题. DP边界:无.区间间隔为0时,默 ...
- poj 2955 区间dp入门题
第一道自己做出来的区间dp题,兴奋ing,虽然说这题并不难. 从后向前考虑: 状态转移方程:dp[i][j]=dp[i+1][j](i<=j<len); dp[i][j]=Max(dp[i ...
- POJ 2955 区间DP Brackets
求一个括号的最大匹配数,这个题可以和UVa 1626比较着看. 注意题目背景一样,但是所求不一样. 回到这道题上来,设d(i, j)表示子序列Si ~ Sj的字符串中最大匹配数,如果Si 与 Sj能配 ...
- 区间dp好题cf149d 括号匹配
见题解链接https://blog.csdn.net/sdjzping/article/details/19160013 #include<bits/stdc++.h> using nam ...
- 区间DP(入门)括号匹配
https://www.nitacm.com/problem_show.php?pid=8314 思路:类似于https://blog.csdn.net/MIKASA3/article/details ...
- poj 3280(区间DP)
Cheapest Palindrome Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7869 Accepted: 38 ...
- poj 2955 Brackets dp简单题
//poj 2955 //sep9 #include <iostream> using namespace std; char s[128]; int dp[128][128]; int ...
- POJ 1651 (区间DP)
题目链接: http://poj.org/problem?id=1651 题目大意:加分取牌.如果一张牌左右有牌则可以取出,分数为左牌*中牌*右牌.这样最后肯定还剩2张牌.求一个取牌顺序,使得加分最少 ...
- POJ 1141 区间DP
给一组小括号与中括号的序列,加入最少的字符,使该序列变为合法序列,输出该合法序列. dp[a][b]记录a-b区间内的最小值, mark[a][b]记录该区间的最小值怎样得到. #include &q ...
随机推荐
- Git常用命令(二)
git clone + URL 获取该项目源代码 $ git log 回顾提交历史 $ git log -p -(数字) 用来显示每次提交的内容差异+(数字)表示看最近几条 $ git log --s ...
- 关于web数据库的相关知识点的操作
主要是怎样建立与数据库的连接.对于框架是固定的: 下面是对于数据库的连接操作: package com.DBU; //数据库连接 import java.sql.Connection; import ...
- 学习《深入应用c++11》2
&& universal references(未定的引用类型),它必须被初始化,它是左值还是右值取决于它的初始化,如果&&被一个左值初始化,它就是一个左值;如果它 ...
- HTTP协议经典面试题整理及答案详解
无论你是Java.PHP开发者,还是运维人员,只要从事互联网行业,面试时都可能被问到HTTP协议相关知识.历时多天的呕心沥血,为你总结了HTTP协议的经典面试题.由于涉及内容比较繁杂不方便记忆,建议收 ...
- scala_spark实践2
参考:jianshu.com/p/9d2d225c1951 监听socket获取数据,代码如下:这里使用nc -lk 9999 在ip为10.121.33.44的机器上发送消息 object Sock ...
- Python财经数据接口包TuShare的使用
安装TuShare 方式1:pip install tushare 方式2:访问https://pypi.python.org/pypi/tushare/下载安装 方式3:将源代码下载到本地pytho ...
- Git应用详解第八讲:Git标签、别名与Git gc
前言 前情提要:Git应用详解第七讲:Git refspec与远程分支的重要操作 这一节主要介绍Git标签.别名与Git的垃圾回收机制. 一.Git标签(tag) 1.标签的实质 标签与分支十分相似, ...
- Springboot系列(七) 集成接口文档swagger,使用,测试
Springboot 配置接口文档swagger 往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配 ...
- JAVA—SQL注入
之前看到的一道java面试题,Statement与PreparedStatement的区别,什么是SQL注入,如何防止SQL注入 前面部分比较好回答 1.PreparedStatement支持动态设置 ...
- 【selenium】各种exception
selenium中的Exception解释 exception selenium.common.exceptions.ElementClickInterceptedException(msg=None ...