POJ-2955 Brackets(括号匹配问题)
题目链接:http://poj.org/problem?id=2955
这题要求求出一段括号序列的最大括号匹配数量
规则如下:
- 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
分析题目的时候发现,这个和回文子序列统计有点类似,当i,j匹配时,它就可以从区间i+1,j-1的最大匹配数转移过来。除此之外,无论是否匹配,都可以从区间[i,k]+区间[k+1,j]求和而来(这里不用担心k会打断括号匹配导致数量减少,因为枚举k的过程中,一定会找到括号匹配的边界)。
所及构造dp[i][j]表示区间内的最大匹配数量,转移方式见代码。
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <cstring>
#define LL long long int
using namespace std; const int mod=;
int dp[][];
string s;
bool ok(int i,int j)
{
if(s[i]=='['&&s[j]==']')
return true;
if(s[i]=='('&&s[j]==')')
return true;
return false;
} int main(){
int n,k;
cin.sync_with_stdio(false);
while(cin>>s)
{
if(s=="end")
break;
for(int i=;i<s.length();i++)
fill(dp[i],dp[i]+,);
for(int i=s.length()-;i>=;i--)
for(int j=;j<s.length();j++)
{
if(i>=j)
continue;
if(ok(i,j))
{
if(i+==j)
dp[i][j]=;
else
{
dp[i][j]=dp[i+][j-]+;
for(int k=i;k<=j;k++)
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+][j]);
}
}
else
{
if(i+!=j)
for(int k=i;k<=j;k++)
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k+][j]);
}
}
cout<<dp[][s.length()-]<<endl;
} return ;
}
POJ-2955 Brackets(括号匹配问题)的更多相关文章
- POJ 2955 Brackets(括号匹配一)
题目链接:http://poj.org/problem?id=2955 题目大意:给你一串字符串,求最大的括号匹配数. 解题思路: 设dp[i][j]是[i,j]的最大括号匹配对数. 则得到状态转移方 ...
- poj 2955 Brackets 括号匹配 区间dp
题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+ ...
- POJ - 2955 Brackets括号匹配(区间dp)
Brackets We give the following inductive definition of a “regular brackets” sequence: the empty sequ ...
- poj 2955 Brackets
题目链接:http://poj.org/problem?id=2955 思路:括号匹配问题,求出所给序列中最长的可以匹配的长度(中间可以存在不匹配的)例如[(])]有[()]符合条件,长度为4 dp[ ...
- poj 2955 Brackets (区间dp 括号匹配)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- Poj 2955 brackets(区间dp)
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7795 Accepted: 4136 Descript ...
- POJ 2955 Brackets(区间DP)题解
题意:问最多有几个括号匹配 思路:用dp[i][j]表示i到j最多匹配,若i和j构成匹配,那么dp[i][j] = dp[i + 1][j - 1] + 2,剩下情况dp[i][j] = max(dp ...
- Sereja and Brackets(括号匹配)
Description Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length ...
- poj 2955 Brackets dp简单题
//poj 2955 //sep9 #include <iostream> using namespace std; char s[128]; int dp[128][128]; int ...
随机推荐
- Delphi XE5 for Android (十)
我觉得Delphi最强大的的功能之一就是开发数据库三层应用的DataSnap,在Android上的实现,首先是完成服务器的设计: (1)利用向导完成DataSnap服务器的框架,如下图: 由于是实验, ...
- Java TreeSet的定制排序
注:只贴出实现类 package Test3; import java.util.Comparator;import java.util.TreeSet; public class Test { pu ...
- 4514: [Sdoi2016]数字配对 费用流
链接 https://www.lydsy.com/JudgeOnline/problem.php?id=4514 思路 EK直接贪心做 <0的时候加上剩余返回 二分图a->b的时候 把b- ...
- 【htop】Linux CentOS 6.5下安装htop进程管理工具
一.Htop的使用简介 This is htop, an interactive process viewer for Linux. It is a text-mode application (fo ...
- MyEclipse代码编辑器中汉字太小的解决办法(中文看不清)
问题描述:新安装的myeclipse 2014,代码编辑器中汉字很小看不清 解决办法:调整字体即可.通过菜单Windows——Preferences,输入font过滤选择Colors and Font ...
- python 之 模块
在python模块,是一个python文件,以.py结尾,包含了python对象定义 和python语句 通过import语句 ,语法 import module1[,module2,module3, ...
- ASP.NET Core2.1 中如何使用 Cookie和Session
https://blog.csdn.net/canduecho/article/details/80651853 ASP.NET Core2.1的官方项目模板在创建的Razor Pages和MVC项目 ...
- (zhuan) Notes on Representation Learning
this blog from: https://opendatascience.com/blog/notes-on-representation-learning-1/ Notes on Repr ...
- 【Python】【Web开发】
# [[Web开发]] ''' 最早的软件都是运行在大型机上的,软件使用者通过“哑终端”登陆到大型机上去运行软件.后来随着PC机的兴起,软件开始主要运行在桌面上,而数据库这样的软件运行在服务器端,这种 ...
- 在ETH交易区块链里查看北大的那封信
本文仅限于科普编码知识使用,随便举的例子不代表本人立场. 欢迎在其他网站传播,但转载不得标注来源及作者. 1.随便打开一个ETH区块链浏览网站,比如:https://www.etherchain.or ...