题目链接:http://poj.org/problem?id=2955

题目大意:给你一串字符串,求最大的括号匹配数。

解题思路:

设dp[i][j]是[i,j]的最大括号匹配对数。

则得到状态转移方程:

if(str[i]=='('&&str[j]==')'||(str[i]=='['&&str[j]==']')){
  dp[i][j]=dp[i+1][j-1]+1;
}
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]) ,(i<=k<j)

代码:

 #include<cstdio>
#include<cmath>
#include<cctype>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<string>
#define lc(a) (a<<1)
#define rc(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define clr(arr,val) memset(arr,val,sizeof(arr))
#define _for(i,start,end) for(int i=start;i<=end;i++)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long LL;
const int N=5e2+;
const int INF=0x3f3f3f3f;
const double eps=1e-; int dp[N][N];
char str[N]; int main(){
while(~scanf("%s",str)&&strcmp(str,"end")){
int n=strlen(str);
memset(dp,,sizeof(dp));
for(int len=;len<n;len++){
for(int i=;i+len<n;i++){
int j=i+len;
if(str[i]=='('&&str[j]==')'||(str[i]=='['&&str[j]==']')){
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]);
}
}
}
printf("%d\n",dp[][n-]*);
}
return ;
}

POJ 2955 Brackets(括号匹配一)的更多相关文章

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

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

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

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

  3. poj 2955 Brackets

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

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

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

  5. POJ-2955 Brackets(括号匹配问题)

    题目链接:http://poj.org/problem?id=2955 这题要求求出一段括号序列的最大括号匹配数量 规则如下: the empty sequence is a regular brac ...

  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. QT 设置菜单图标

    目录 一.添加主窗口菜单 二.添加菜单图标 三.添加资源文件 四.使用资源文件 五.使用代码来添加菜单和图标     正文 一.添加主窗口菜单 1.新建Qt Gui应用,项目名称为myMainWind ...

  2. python代码自动补全配置及Django入门Demo

    django入门代码示例小博客:https://pan.baidu.com/s/1pLjLPSv 1.自动补全功能 许多人都知道 iPython 有很好的自动补全能力,但是就未必知道 python 也 ...

  3. Swarm使用原生的overlay网络

    一.Swarm Overlay Network Swarm有Service的概念.一个Service是指使用相同镜像.同时运行的多个容器,多个容器同时一起对外提供服务,多个容器之间负载均衡.每个Ser ...

  4. Ansible12:lookup

    目录 简单说明 1.file 2.pipe 3.env 4.template 5.csvfile 6.redis_kv 7.etcd 8.password 9.dnstxt 简单说明 在通常情况下,所 ...

  5. Mac 远程连接windows桌面工具parallels client

    https://www.cnblogs.com/cssfirefly/p/9644940.html Parallels Client 客户端是免费,App Store 直接下载 复制文件:用共享文件夹 ...

  6. 关于构造IOCTL命令的学习心得

    在编写ioctl代码之前,需要选择对应不同命令的编号.为了防止对错误的设备使用正确的命令,命令号应该在系统范围内唯一,这种错误匹配并不是不会发生,程序可能发现自己正在试图对FIFO和audio等这类非 ...

  7. nandflash,norflash,sdram,emmc,rom,ram等各种存储器识别

    老是被nandflash,norflash,sdram,emmc,rom,ram搞混,所以在这里总结一下,也为了更好的分清他们之间的关系,以至于别人问的时候不至于说不清. 我们不谈这些名次的由来,只说 ...

  8. src.rpm包的解压

    有时候,我们在找源码包时候,发现有src.rpm的包:而不是tar.gz/tgz/zip结尾的. 那么如何去看这个src.rpm里面的详细信息呢? 看完下面这个例子,基本上明白了. 1,首先,生成sp ...

  9. Android的音频解码原来是直接调用的本地C方法直接通过硬件解码

    Android就是披着JAVA外衣的C啊~音频解码原来是直接调用的本地C方法直接通过硬件解码的,JAVA和C的字节数组存放模式不同(java是大端,C根据不同平台不同),不同格式需要转化以后才能用. ...

  10. idea插件安装的通用操作

    序:今天下午看到一个bug,很神奇,粘出来大家看看 看到这个异常栈,有经验的或者查到的答案都是mapper.xml中哪个的方法配置错了,应替换parameterMap为parameterType, 奇 ...