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 ...
随机推荐
- 【剑指Offer】48、不用加减乘除做加法
题目描述: 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 解题思路: 本题同样是对发散思维能力的一个考察.首先,我们需要考虑是要求和却不能使用四则运算 ...
- 被遗忘的 Logrotate
转自: http://huoding.com/2013/04/21/246 被遗忘的 Logrotate 发表于 2013-04-21 我发现很多人的服务器上都运行着一些诸如每天切分 Nginx 日志 ...
- 【ABCD组】Scrum meeting 3
前言 第3次会议在6月15日由组长在教9 405召开. 主要对下一步的工作进行说明安排,时长90min. 主要内容 讨论怎么用c#进行下一步系统的完成 任务分配 姓名 当前阶段任务 贡献时间 下阶段任 ...
- JavaSE 学习笔记之封装(四)
封 装(面向对象特征之一):是指隐藏对象的属性和实现细节,仅对外提供公共访问方式. 好处:将变化隔离:便于使用:提高重用性:安全性. 封装原则:将不需要对外提供的内容都隐藏起来,把属性都隐藏,提供公共 ...
- ASP.NET--IIS的Http请求流程
下面的文章是基于IIS经典模式给出的流程图,和IIS集成模式有些区别,WIN7系统自带的是IIS7,和文章分写的有些区别,现在基本上都用IIS7了,所以不要入坑 伯乐在线的文章一 伯乐在线的文章二
- HDU 5338 ZZX AND PERMUTATIONS 线段树
pid=5338" target="_blank" style="text-decoration:none; color:rgb(45,125,94); bac ...
- C++二叉树笔试题
#include <iostream> #include <stack> #include <queue> using namespace std; templat ...
- 固定管线shader编写:基本属性
欢迎转载!转载时请注明出处:http://write.blog.csdn.net/postedit/50753008 shader 部分介绍: properties:属性部分 material:材质部 ...
- 网络 - 网关的作用、DNS的作用
DNS的作用 域名系统.负责把域名翻译成ip,或者把ip翻译成域名. hosts文件用于静态的域名解析.优先级高于DNS解析. DNS服务器,负责解析域名到ip地址上. 114.114.114.114 ...
- [Pulgin] 前端上传组件Plupload使用指南
我之前写过一篇文章<文件上传利器SWFUpload使用指南>,里面介绍了上传组件SWFUpload的使用方法,但现在随着html5技术的逐渐推广和普及,再去使用以flash为上传手段的SW ...