POJ 2955 (区间DP)】的更多相关文章

第一道自己做出来的区间dp题,兴奋ing,虽然说这题并不难. 从后向前考虑: 状态转移方程:dp[i][j]=dp[i+1][j](i<=j<len); dp[i][j]=Max(dp[i][j],dp[i+1][k-1]+dp[k+1][j]+1),(a[i]==a[j]&&i<len,j<len,k<len); #include<stdio.h> #include<string.h> #define N 300 int dp[N][…
题目链接: http://poj.org/problem?id=2955 题目大意:括号匹配.对称的括号匹配数量+2.问最大匹配数. 解题思路: 看起来像个区间问题. DP边界:无.区间间隔为0时,默认为memset为0即可. 对于dp[i][j],如果i和j匹配,不难有dp[i][j]=dp[i+1][j-1]+2. 然后枚举不属于两端的中点, dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]),合并两个区间的结果. #include "cstdio"…
Brackets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14226 Accepted: 7476 Description We give the following inductive definition of a "regular brackets" sequence: the empty sequence is a regular brackets sequence, if s is a regula…
求一个括号的最大匹配数,这个题可以和UVa 1626比较着看. 注意题目背景一样,但是所求不一样. 回到这道题上来,设d(i, j)表示子序列Si ~ Sj的字符串中最大匹配数,如果Si 与 Sj能配对,d(i, j) = d(i+1, j-1) 然后要枚举中间点k,d(i, j) = max{ d(i, k) + d(k+1, j) } #include <iostream> #include <cstdio> #include <cstring> #include…
//poj 2955 //sep9 #include <iostream> using namespace std; char s[128]; int dp[128][128]; int n; int rec(int l,int r) { if(dp[l][r]!=-1) return dp[l][r]; if(l==r) return dp[l][r]=0; if(l+1==r){ if(s[l]=='('&&s[r]==')') return dp[l][r]=2; if(…
Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7869   Accepted: 3816 Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow a…
题目链接: http://poj.org/problem?id=1651 题目大意:加分取牌.如果一张牌左右有牌则可以取出,分数为左牌*中牌*右牌.这样最后肯定还剩2张牌.求一个取牌顺序,使得加分最少. 解题思路: 矩阵链乘的变种题. 假设有10.20.30.40.50五张牌. 如果我想要最后取30,则应该先取20.40,这样就还剩10.30.50三张牌了. 不难发现取20是dp[i][k]部分,取40是dp[k][j]部分,最后剩下的就是i.k.j三张牌. DP边界:无 因为是算加分,区间间隔…
给一组小括号与中括号的序列,加入最少的字符,使该序列变为合法序列,输出该合法序列. dp[a][b]记录a-b区间内的最小值, mark[a][b]记录该区间的最小值怎样得到. #include "stdio.h" #include "string.h" int inf=99999999; char str[110]; int dp[110][110],mark[110][110]; void pri(int l,int r) { if (l>r) retur…
Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5035   Accepted: 2065 Description Some of you may have played a game called 'Blocks'. There are n blocks in a row, each box has a color. Here is an example: Gold, Silver, Silver, Sil…
此题可以转化为最优矩阵链乘的形式,d(i, j)表示区间[i, j]所能得到的最小权值. 枚举最后一个拿走的数a[k],状态转移方程为d(i, j) = min{ d(i, k) + d(k, j) + a[i] * a[k] * a[j] } #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; + ; const…