poj 1141 区间dp+递归打印路径】的更多相关文章

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30383   Accepted: 8712   Special Judge Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a re…
题目传送门 题意:n个饭店在一条直线上,给了它们的坐标,现在要建造m个停车场,饭店没有停车场的要到最近的停车场,问所有饭店到停车场的最短距离 分析:易得区间(i, j)的最短距离和一定是建在(i + j) / 2的饭店,预处理出(i, j)的距离和sum[i][j],mark[i][j] 表示区间的最优停车场的位置,mid[i][j]表示(i + j) / 2.状态转移方程:dp[i][j] = max (dp[k-1][j-1] + sum[k][i]): 收获:学习递归打印路径 代码: /*…
uva 紫书例题,这个区间dp最容易错的应该是(S)这种匹配情况,如果不是题目中给了提示我就忽略了,只想着左右分割忘记了这种特殊的例子. dp[i][j]=MIN{dp[i+1][j-1] | if(match(i,j) , dp[i][k]+dp[k+1][j] | i<=k<=j .}注意初始化dp[i][i]=1,表示1个字符最少需要一个才能匹配,dp[i+1][i]=0,因为可能只有两个字符使得i+1>j-1,我们可以认为中间是空字符已经匹配了. 打印路径利用了递归,很巧妙,lr…
又几天没写博客了,大二的生活实在好忙碌啊,开了五门专业课,每周都是实验啊实验啊实验啊....我说要本月刷够60题,但好像完不成了,也就每天1题的样子.如今写动规还是挺有条理的,包括这道需要打印轨迹,其实就是在POJ 2955的基础上进行下修改,记录下动规的方向,再用递归逆向输出即可! 是个区间DP,如果 括号i和括号k匹配了,则 dp[i][j]=max(dp[i][j],dp[i][k]+1+dp[k+1][j])这个转移方程挺重要的,我一开始就是这个方程没写好..弄得思路混乱了 还有就是一开…
给一组小括号与中括号的序列,加入最少的字符,使该序列变为合法序列,输出该合法序列. 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…
  Compromise  In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Ger…
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speed…
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…
第一道自己做出来的区间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"…