POJ 2955:Brackets(区间DP)
http://poj.org/problem?id=2955
题意:给出一串字符,求括号匹配的数最多是多少。
思路:区间DP。
对于每个枚举的区间边界,如果两边可以配对成括号,那么dp[i][j] = dp[i+1][j-1] + 2,表示由上一个状态加上当前的贡献。
然后和普通的区间合并一样去更新。
#include <cstring>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
#define N 105
int dp[][];
string s; int main() {
while(cin >> s) {
if(s == "end") break;
int n = s.length();
memset(dp, , sizeof(dp));
for(int len = ; len < n; len++) {
for(int i = ; i + len < n; i++) {
int j = i + len;
if(s[i] == '(' && s[j] == ')' || s[i] == '[' && s[j] == ']') dp[i][j] = + dp[i+][j-];
for(int k = i; k < j; k++)
if(dp[i][j] < dp[i][k] + dp[k+][j]) dp[i][j] = dp[i][k] + dp[k+][j];
}
}
printf("%d\n", dp[][n-]);
}
return ;
}
POJ 2955:Brackets(区间DP)的更多相关文章
- HOJ 1936&POJ 2955 Brackets(区间DP)
Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...
- poj 2955 Brackets (区间dp基础题)
We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...
- poj 2955"Brackets"(区间DP)
传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给你一个只由 '(' , ')' , '[' , ']' 组成的字符串s[ ], ...
- poj 2955 Brackets (区间dp 括号匹配)
Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...
- POJ 2955 Brackets 区间DP 入门
dp[i][j]代表i->j区间内最多的合法括号数 if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']') dp[i][j] ...
- POJ 2955 Brackets(区间DP)
题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <vector& ...
- POJ 2955 Brackets 区间DP 最大括号匹配
http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...
- POJ 2995 Brackets 区间DP
POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...
- A - Brackets POJ - 2955 (区间DP模板题)
题目链接:https://cn.vjudge.net/contest/276243#problem/A 题目大意:给你一个字符串,让你求出字符串的最长匹配子串. 具体思路:三个for循环暴力,对于一个 ...
- POJ 2955 Brackets 区间合并
输出一个串里面能匹配的括号数 状态转移方程: if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']') dp ...
随机推荐
- doker基础
去 Docker Hub 上拉取一个叫 hello-world 的集装箱docker pull hello-world然后让这个集装箱跑起来docker run hello-world查看本机所安装的 ...
- jquery表单过滤器
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- C++ Boost 学习资源列表
文档书籍下载 Boost Documentation Boost代码下载 优秀网站导航 Boost官方网站 Boost中文站 Boost Consulting 专题资源报告 Lin ...
- js如何通过变量调用函数,函数名在变量里面
js如何通过变量调用函数,函数名在变量里面. 有时候函数名是动态定义的,这时候我们就需要用到这个方法了. //赋值函数名称 var a = "b"; //定义函数 function ...
- UWP使用AppService向另一个UWP客户端应用程序提供服务
原文:UWP使用AppService向另一个UWP客户端应用程序提供服务 在上篇里,我使用的是寄宿在WPF上的WCF进行两个程序间的通信,在解决问题的同时,我的同事也在思考能否使用UWP来做这件事.于 ...
- System.Exception: ORA-12541: TNS: 无监听程序
今天在一个服务器上发布一个web服务(数据库也装在这台机器上):开发工具 Visual Studio 2008 Oracle但是部署好,浏览的时候报错了:System.Web.Services.Pro ...
- [机器学习]Generalized Linear Model
最近一直在回顾linear regression model和logistic regression model,但对其中的一些问题都很疑惑不解,知道我看到广义线性模型即Generalized Lin ...
- 微服务之Service Fabric 系列 (一):概览、环境安装
参考 微软官方文档 service fabric 百家号 大话微服务架构之微服务框架微软ServiceFabric正式开源 一.概述 1.概念 Azure Service Fabric 是一款分 ...
- Office Add-In 应用类型及平台支持
原文地址: http://simpeng.net/office-add-in/office-add-in-%e5%ba%94%e7%94%a8%e7%b1%bb%e5%9e%8b%e5%8f%8a%e ...
- qt5.7交叉编译gstreamer-1.0
一.交叉编译glib1.提前需先交叉编译libffiCC=/home/mjl/opt/gcc-4.7-linaro-rpi-gnueabihf/bin/arm-linux-gnueabihf-gcc ...