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 ...
随机推荐
- WPF中Binding使用StringFormat格式化字符串方法
原文:WPF中Binding使用StringFormat格式化字符串方法 货币格式 <TextBlock Text="{Binding Price, StringFormat={}{0 ...
- WPF 视图导航
<Window x:Class="ViewExam.MainWindow" xmlns="http://schemas.microsoft.com/w ...
- JAVASCRIPT高程笔记-------第五章 引用类型
一.Object类型 1.1创建方式 ①new关键字 : var person = new Oject(); ②给定直接量: var person = { name : "zhangsan& ...
- WCF调试日志
WCF调试,打不了断点or远程调试时,在配置文件的<configuration>结点下面加一段,就可以在对应位置查看服务器调试日志了,远程调试完毕发送亦可! <system.diag ...
- C#基础:ref和out的区别 [转]
ref和out的区别在C# 中,既可以通过值也可以通过引用传递参数.通过引用传递参数允许函数成员更改参数的值,并保持该更改.若要通过引用传递参数, 可使用ref或out关键字.ref和out这两个关键 ...
- swift 如何控制view的显示与隐藏
swift 如何控制view的显示与隐藏 UIView有一个属性 hidden let line: UILabel = UILabel() 默认是显示的 需要显示它的时候:line.hidden = ...
- HLS(HTTP Live Streaming)学习和探讨
Introduction HTTP Live Streaming lets you send audio and video over HTTP from an ordinary web server ...
- 笔记:Advanced Installer 打包Web应用
原文:笔记:Advanced Installer 打包Web应用 公司要做一款增值税小产品,区别于ACME,本产品核心只有销项部分,面对的客户群是小企业,单税盒单开票机..... 我要做的主要有以下几 ...
- Dec Working Note
01 新的一个月,也是16年最后一个月,意义非凡. 那么第一天就要来点非凡的意义:提出离职. 纠结了好久,最后还是离职了,感觉是好他妈的爽,纠结什么呢. 不过今天状态不好,最近状态一直不好,上火,也没 ...
- 16.Oct Working Note
01 writing algorithm by assembly,but the bug... now,it runs normaly,but how to print the answer? suc ...