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 ...
随机推荐
- eas之常用源码整理
//查看是否有相关权限 boolean hasAllotPermission= PermissionFactory.getRemoteInstance().hasFunctionPer ...
- [luogu2825 HEOI2016/TJOI2016] 游戏 (二分图最大匹配)
传送门 Description 在2016年,佳缘姐姐喜欢上了一款游戏,叫做泡泡堂.简单的说,这个游戏就是在一张地图上放上若干个炸弹,看是否能炸到对手,或者躲开对手的炸弹.在玩游戏的过程中,小H想到了 ...
- nginx下部署showdoc
1. 安装nginx服务器sudo apt-get install nginx -y 2. 启动服务sudo service nginx start 3. 安装php环境 sudo apt-get i ...
- Python语言认识和实用工具(1)
目录 1. Python语言概述 2. Python使用注意事项 3. Python学习工具 1. Python语言概述 Python是一种计算机程序设计语言.是一种动态的.面向对象的脚本语言,语 ...
- delphi窗口的create和free,一个古老的话题
窗体分为模式窗体和无模式窗体. 模式窗体在创建窗口创建和释放: begin if not Assigned(FB_Input_JianYanDan) then FB_Input_JianYanDan ...
- 洛谷 P3178 BZOJ 4034 [HAOI2015]树上操作
题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个操作,分为三种:操作 1 :把某个节点 x 的点权增加 a .操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 ...
- 敏捷迭代:Sprint燃尽图的7个图形特征及说明的问题
本文写于很多年前(2006),并在很多地方被引用.而现在,笔者对于Sprint燃尽图的理解有了戏剧性的变化--在看到很多团队滥用它之后.笔者不再建议团队做Sprint燃尽图,因为它们不仅不会增加多少有 ...
- Find the Clones Trie Tree
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8306 Accepted: 3130 Description Doubl ...
- hdu1018--斯特灵公式
斯特灵公式 Wiki http://zh.wikipedia.org/wiki/斯特林公式 /** \brief hdu 1018 * * \param date 2014/7/24 * \param ...
- LeetCode(1) Symmetric Tree
从简单的道题目開始刷题目: Symmetric Tree 题目:Given a binary tree, check whether it is a mirror of itself (ie, sym ...