poj 2955 Brackets 【 区间dp 】
话说这题自己折腾好久还是没有推出转移的公式来啊------------------
只想出了dp[i][j]表示i到j的最大括号匹配的数目--ค(TㅅT)-------------------
后来搜题解看到有两种有一点点不同的做法
dp[i][j] = max(dp[i+1][j-1] + ok(i,j), dp[i][k] + dp[k+1][j])
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int d[][];
char s[];
int n; int ok(int x,int y){
if(s[x] == '(' && s[y] == ')') return ;
if(s[x] == '[' && s[y] == ']') return ;
return ;
} int dp(int x,int y){
int& ans = d[x][y];
if(ans >= ) return ans;
if(x > y) return ; ans = dp(x+,y-) + ok(x,y);
for(int k = x;k < y;k++){
ans = max(ans,dp(x,k) + dp(k+,y));
// printf("ans = %d\n",ans);
}
return ans;
} int main(){
while(scanf("%s",s+) != EOF){
if(s[] == 'e') break;
n = strlen(s+); memset(d,-,sizeof(d)); int res = dp(,n); // for(int i = 1;i <= n;i++)
// for(int j = 1;j <= n;j++)
// printf("d[%d][%d] = %d\n",i,j,d[i][j]); printf("%d\n",*res);
}
return ;
}
另一种是考虑i的作用,感觉和换衣服那题有点点像
如果i只被第i个位置用,不考虑后来和它配对的,dp[i][j] = dp[i+1][j]
考虑i和后面的配对,dp[i][j] = max(dp[i][j],dp[i+1][k-1] + dp[k+1][j] + ok(i,k) );
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; int d[][];
char s[];
int n; int ok(int x,int y){
if(s[x] == '(' && s[y] == ')') return ;
if(s[x] == '[' && s[y] == ']') return ;
return ;
} int dp(int x,int y){
int& ans = d[x][y];
if(ans >= ) return ans;
if(x >= y) {
ans = ;
return ans;
}
if(y == x+) {
ans = ok(x,y);
return ans;
} ans = dp(x+,y);
for(int k = x;k <= y;k++){
if(ok(x,k)) ans = max(ans,dp(x+,k-) + dp(k+,y) + );
// printf("ans = %d\n",ans);
}
return ans;
} int main(){
while(scanf("%s",s+) != EOF){
if(s[] == 'e') break;
n = strlen(s+); memset(d,-,sizeof(d)); int res = dp(,n); //for(int i = 1;i <= n;i++)
//for(int j = 1;j <= n;j++)
// printf("d[%d][%d] = %d\n",i,j,d[i][j]); printf("%d\n",res);
}
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 ...
随机推荐
- sass使用中出现的问题
问题一:ruby按照官方文档安装后更换gem源时,报错Error fetching https://gems.ruby-china.org/: bad response Not Found 404 ( ...
- 61.index CUD
主要知识点 索引CUD 一.创建索引的语法 PUT /my_index { "settings": { ... any settings ... }, " ...
- Problem 16
Problem 16 pow(2, 15) = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.2的15次方等于32768,而这些数 ...
- EasyUI Messager 消息框点击右上角x无法执行回掉函数解决
我先声明下,我是改动源码,网上的其他方法我试过了,不行.我是写java后台的,前端也是小白,可能讲的也也不是很清楚. 首先我想到的是在网上找到解决方法,我找到一个方法说在jquery.easyui.m ...
- 浅谈 Mysql
数据库一些基本的概念 Mysql 基础操作 Mysql 数据类型 Mysql 表的完整性约束 Mysql 表的操作 Mysql 数据增删改查语句 Mysql 多表查询 Mysql 索引原理及分类 My ...
- cxdbtreelist的按记录查找节点
lst_projet.DataController.DataSet.Locate('pm_id',vPm_ID,[]); bl:= lst_projet.DataController.DataSet. ...
- Java开发编码规范
第一章 代码开发规范及其指南 一.1 目的 定义这个规范的目的是让项目中所有的文档都看起来像一个人写的,增加可读性,减少项目组中因为换人而带来的损失.(这些规范并不是一定要绝对遵守,但是一定要让程序有 ...
- Disruptor使用
Disruptor作者,介绍Disruptor能每秒处理600万订单.这是一个可怕的数字. disruptor之所以那么快,是因为内部采用环形队列和无锁设计.使用cas来进行并发控制.通过获取可用下标 ...
- 0709关于mysql优化思路【何登成】
转自 http://isky000.com/database/mysql-performance-tuning-sql 优化目标 减少 IO 次数IO永远是数据库最容易瓶颈的地方,这是由数据库的职责所 ...
- nginx配置文件使用
nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; 全局错误日志定义类型,[ debug | info | notice | warn | error | cri ...