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 ...
随机推荐
- Docker 入门指南——Dockerfile 指令
COPY 复制文件 格式: COPY [--chown=<user>:<group>] <源路径>... <目标路径> 源路径可以是多个,甚至可以使通配 ...
- 关于linux下自定义的 alias文件和自定义函数库的通用写法(只适合自己的)
使用alias和自定义的function的必要性和重要性就不说了 , 自己的通用做法是: 可以创建: ~/bin/my.alias 文件 作为自定义的 alias专门文件 创建: ~/libsh/my ...
- UVALive 7503 Change(乱搞)题解
题意:你现在有面额为A的纸币,现在需要面额为B的钱(可以是一张也可以是好多张拼成一张),有一台自动售货机,里面有任意价格的商品,售货机兑换出的零钱是随机的(比如找你0.03可能给你0.01+0.01+ ...
- 【分布式事务】spring cloud集成lcn解决分布式事务
参考地址:https://blog.csdn.net/u010882691/article/details/82256587 参考地址:https://blog.csdn.net/oyh1203/ar ...
- awk - group adjacent rows by identical columns
Liang always brings me interesting quiz questions. Here is one: If i have a table like below: chr1 1 ...
- SPOJ 687 REPEATS - Repeats
题意 给定字符串,求重复次数最多的连续重复子串 思路 后缀数组的神题 让我对着题解想了快1天 首先考虑一个暴力,枚举循环串的长度l,然后再枚举每个点i,用i和i+l匹配,如果匹配长度是L,这个循环串就 ...
- The more... the more句型
百度文库:https://wenku.baidu.com/view/a7f1067f59fb770bf78a6529647d27284a73374b.html the more ... , the m ...
- shell script 脚本编程
介绍 Shell脚本编程30分钟入门 Shell 教程 Linux 的 Shell 种类众多,常见的有: Bourne Shell(/usr/bin/sh或/bin/sh) Bourne Again ...
- Latex: 设置 threeparttable footnote 行宽度
参考: Table width with threeparttable smaller than notes and caption? Latex: 设置 threeparttable footnot ...
- 17秋 软件工程 团队第三次作业 预则立&他山之石
题目:团队作业-预则立&&他山之石 团队: 我说嘻(xì)哈(hà)你说侠 17秋 软件工程 团队第三次作业 预则立&他山之石 1.确立团队选题,建立和初步熟悉团队git的协作 ...