题目链接: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(括号匹配问题)的更多相关文章

  1. POJ 2955 Brackets(括号匹配一)

    题目链接:http://poj.org/problem?id=2955 题目大意:给你一串字符串,求最大的括号匹配数. 解题思路: 设dp[i][j]是[i,j]的最大括号匹配对数. 则得到状态转移方 ...

  2. poj 2955 Brackets 括号匹配 区间dp

    题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+ ...

  3. POJ - 2955 Brackets括号匹配(区间dp)

    Brackets We give the following inductive definition of a “regular brackets” sequence: the empty sequ ...

  4. poj 2955 Brackets

    题目链接:http://poj.org/problem?id=2955 思路:括号匹配问题,求出所给序列中最长的可以匹配的长度(中间可以存在不匹配的)例如[(])]有[()]符合条件,长度为4 dp[ ...

  5. poj 2955 Brackets (区间dp 括号匹配)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  6. Poj 2955 brackets(区间dp)

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7795   Accepted: 4136 Descript ...

  7. 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 ...

  8. Sereja and Brackets(括号匹配)

    Description Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length  ...

  9. poj 2955 Brackets dp简单题

    //poj 2955 //sep9 #include <iostream> using namespace std; char s[128]; int dp[128][128]; int ...

随机推荐

  1. Python3基础 list count 查询指定元素在列表中出现了多少次

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. linux内核中宏likely和unlikely到底做了些什么?

    1. 先看看它们长啥样吧!(它们有两种定义,第一种是使能了程序trace功能的宏定义,第二种是普通的宏定义,咱们分析普通宏定义吧) # define likely(x) __builtin_expec ...

  3. aria2的下载配置

    aria2的命令行命令是: aria2c 一种方式: aria2c "http://host/file.zip" 如同wget 第二种方式: rpc server方式:

  4. (转)Is attacking machine learning easier than defending it?

    转自:http://www.cleverhans.io/security/privacy/ml/2017/02/15/why-attacking-machine-learning-is-easier- ...

  5. sql server数据库自动备份

    SqlServer 数据库自动备份的两种解决方案 SQL Server中的角色(服务器级别和数据库级别角色) SqlServer服务器角色和数据库角色相关操作

  6. Leetcode121-Best Time to Buy and Sell Stock I - Easy

    I Say you have an array for which the ith element is the price of a given stock on day i. If you wer ...

  7. 那些年我们一起用过的 PHP 函数(一)

    用这个帖子记录一下 PHP 中比较常用的函数以及使用过程中需要注意的点,此贴持续更新 1.strpos() 函数     首先,我们知道 strpos() 函数是用于查找一个字符串在另一个字符串第一次 ...

  8. Go 定长的数组

    1.Go 语言数组的简介 几乎所有的计算机语言都有数组,应用非常的广泛.同样,在 Go 语言中也有数组并且在数组的基础上还衍生出了切片(slice). 数组是一系列同一类型数据的集合,数组中包含的每个 ...

  9. particles.js使用及配置

    particles.js使用及配置 参考:http://blog.csdn.net/csdn_yudong/article/details/53128570 这个项目中有提供demo,可以直接下载这个 ...

  10. hdu 3864 D_num Pollard_rho算法和Miller_Rabin算法

    D_num Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem De ...