话说这题自己折腾好久还是没有推出转移的公式来啊------------------

只想出了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 】的更多相关文章

  1. HOJ 1936&POJ 2955 Brackets(区间DP)

    Brackets My Tags (Edit) Source : Stanford ACM Programming Contest 2004 Time limit : 1 sec Memory lim ...

  2. poj 2955 Brackets (区间dp基础题)

    We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...

  3. poj 2955"Brackets"(区间DP)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给你一个只由 '(' , ')' , '[' , ']' 组成的字符串s[ ], ...

  4. poj 2955 Brackets (区间dp 括号匹配)

    Description We give the following inductive definition of a “regular brackets” sequence: the empty s ...

  5. POJ 2955 Brackets 区间DP 入门

    dp[i][j]代表i->j区间内最多的合法括号数 if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']') dp[i][j] ...

  6. POJ 2955 Brackets(区间DP)

    题目链接 #include <iostream> #include <cstdio> #include <cstring> #include <vector& ...

  7. POJ 2955 Brackets 区间DP 最大括号匹配

    http://blog.csdn.net/libin56842/article/details/9673239 http://www.cnblogs.com/ACMan/archive/2012/08 ...

  8. POJ 2995 Brackets 区间DP

    POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...

  9. A - Brackets POJ - 2955 (区间DP模板题)

    题目链接:https://cn.vjudge.net/contest/276243#problem/A 题目大意:给你一个字符串,让你求出字符串的最长匹配子串. 具体思路:三个for循环暴力,对于一个 ...

  10. POJ 2955 Brackets 区间合并

    输出一个串里面能匹配的括号数 状态转移方程: if(s[i]=='('&&s[j]==')'||s[i]=='['&&s[j]==']')             dp ...

随机推荐

  1. 数据类型---->Number

    Javascript中有六种数据类型 1.undefined:这个值未定义 2.boolean:这个值是布尔值 3.number:这个值是数值 4.function:这个值是函数 5.object:这 ...

  2. 06.系统编程-4.多线程和GIL

    为什么有人会说 Python? 多线程是鸡肋?知乎上有人提出这样一个问题,在我们常识中,多进程.多线程都是通过并发的方式充分利用硬件资源提高程序的运行效率,怎么在 Python 中反而成了鸡肋? 有同 ...

  3. vue_music:排行榜rank中top-list.vue中样式的实现:class

    排行榜的歌曲列表,根据排名渲染不同的样式,同时需要考虑分辨率的2x 3x图 不同的样式--:class 考虑分辨率的2x 3x图--需要在stylu中的mixin中bgImage根据屏幕分辨率选择图片 ...

  4. 继续聊WPF

    下面看一个Tick控件的例子,这只是演示,Tick单独使用没有意义. <TickBar Height="15" Width="180" Ticks=&qu ...

  5. Redis启动出错 noauth authentication required.

    输入认证过的密码即可. 在命令行中运行: auth password

  6. opencv学习HighGUI图形用户界面初步【1】

    HighGUI是图形用户界面模块.包括:1.输入与输出:2.视频捕捉:3.图形和视频的解码编码:4.图形交界面与接口. 由于opencv.hpp包含了core.objdetect.ingproc.ph ...

  7. 【例题 4-5 uva 512】Spreadsheet Tracking

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个操作对与一个点来说变化是固定的. 因此可以不用对整个数组进行操作. 对于每个询问,遍历所有的操作.对输入的(x,y)进行相应的变 ...

  8. 【[Offer收割]编程练习赛11 B】物品价值

    [题目链接]:http://hihocoder.com/problemset/problem/1486 [题意] [题解] 设f[i][j]表示前i个物品,每种属性的状态奇偶状态为j的最大价值; 这里 ...

  9. Merging into a Table: Example

    Merging into a Table: Example The following example uses the bonuses table in the sample schema oe w ...

  10. Atomic operations on the x86 processors

    On the Intel type of x86 processors including AMD, increasingly there are more CPU cores or processo ...