题目链接: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. Android 源码编译 指定userdata.img、system.img、cache.img容量大小【转】

    本文转载自:https://blog.csdn.net/baodinglaolang/article/details/49791041 修改build/target/board/generic_x86 ...

  2. Sql 通过表名查找所有列名

    SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'ImmediacyOutKu'

  3. POJ 2409 Let it Bead

    思路 同这道题,只是颜色数从3变成c 代码 #include <cstdio> #include <algorithm> #include <cstring> #d ...

  4. CF600E Lomsat gelral(dsu on tree)

    dsu on tree跟冰茶祭有什么关系啊喂 dsu on tree的模板题 思想与解题过程 类似树链剖分的思路 先统计轻儿子的贡献,再统计重儿子的贡献,得出当前节点的答案后再减去轻儿子对答案的贡献 ...

  5. CF983B XOR-pyramid

    设\(xorx[l][r]\)表示题目中\(f(l,r)\)的值,则可以得出 \[ xorx[i][j]=xorx[i][j-1] \oplus xorx[i+1][j] \] 设\(maxx[l][ ...

  6. 题解——HDU 4734 F(x) (数位DP)

    这道题还是关于数位DP的板子题 数位DP有一个显著的特征,就是求的东西大概率与输入关系不大,理论上一般都是数的构成规律 然后这题就是算一个\( F(A) \)的公式值,然后求\( \left [ 0 ...

  7. Vue的生命周期(钩子函数)

    Vue一共有10个生命周期函数,我们可以利用这些函数在vue的每个阶段都进行操作数据或者改变内容. 其实在Vue的官网有一张图已经很好的诠释了生命周期,我在这里就不再多讲了,直接贴图,然后上程序代码. ...

  8. (转载)WinCC 卸载后 Simatic Shell 的删除

    现象:WinCC卸载后,在计算机(我的电脑)中仍有Simatic Shell文件夹,双击无反应 解决:1.按Win+X,运行“regedit”,打开注册表2.在注册表中,选中HKEY_LOCAL_MA ...

  9. 第一章(欢迎进入node.js世界)

    本章内容 1:Node.js是什么 2:服务器端javascript 3:node的异步和事件触发本质 4:node为谁而生 5:node程序示例 1.1 node.js他的首次亮相是在2009年,非 ...

  10. JavaScript——语法与数据类型

    严格模式 ECMA5引入了严格模式的概念.严格模式是为JavaScript定义了一种不同的解析与执行模型.在严格模式下,ECMA3中的一些不确定的行为将得到处理,而且对某些不安全的操作也会抛出错误.要 ...