
ACM-动态规划
文章平均质量分 73
LarryNLPIR
专注NLP/IR/Machine Learning/Data Mining
展开
-
LeetCode Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ /原创 2015-08-31 12:42:57 · 2859 阅读 · 0 评论 -
LeetCode Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) I原创 2014-12-31 14:42:12 · 4053 阅读 · 0 评论 -
LeetCode Candy
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one candy.原创 2015-01-12 14:56:12 · 1649 阅读 · 0 评论 -
LeetCode Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to rea原创 2015-01-19 14:40:52 · 1563 阅读 · 0 评论 -
LeetCode Dungeon Game
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially pos原创 2015-01-12 15:52:30 · 3635 阅读 · 0 评论 -
LeetCode Unique Paths II
Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the grid.For e原创 2015-01-27 11:27:14 · 1813 阅读 · 0 评论 -
LeetCode Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest produc原创 2014-11-29 03:00:31 · 2013 阅读 · 0 评论 -
LeetCode Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?思路分析:考察DP,定义ClimbWays数组,ClimbWays[n]原创 2014-11-29 05:25:23 · 1860 阅读 · 0 评论 -
LeetCode Maximum Subarray
LeetCode Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous su原创 2014-10-30 05:39:13 · 2198 阅读 · 0 评论 -
LeetCode Word Break II
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, given s = “catsanddo原创 2015-02-07 12:08:42 · 3795 阅读 · 0 评论 -
LeetCode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) o原创 2015-02-05 13:44:50 · 1541 阅读 · 0 评论 -
LeetCode Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Return原创 2015-07-27 16:09:17 · 3164 阅读 · 0 评论 -
LeetCode House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house原创 2015-04-20 12:33:11 · 3502 阅读 · 0 评论 -
LeetCode Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), des原创 2015-03-29 12:15:13 · 1542 阅读 · 0 评论 -
LeetCode Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one an原创 2015-03-29 12:01:45 · 1507 阅读 · 0 评论 -
LeetCode Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string原创 2015-02-15 13:33:55 · 2528 阅读 · 0 评论 -
LeetCode Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bott原创 2015-01-05 07:13:37 · 1939 阅读 · 1 评论 -
LeetCode Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at any原创 2015-02-23 10:55:30 · 1105 阅读 · 0 评论 -
LeetCode Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet", "code"原创 2015-01-23 13:13:43 · 2547 阅读 · 0 评论 -
LeetCode Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]原创 2014-10-30 05:35:09 · 1498 阅读 · 0 评论 -
LeetCode Triangle
LeetCode TriangleGiven a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3原创 2014-10-30 05:24:45 · 1935 阅读 · 0 评论 -
POJ 3636 俄罗斯Nested Dolls 贪心二分 动态规划
首先要区分#include 中的qsort与C++ STL algorithm中的sort,后者只有三个参数且比较函数写法较简单些 类似于POJ 1065,差异有二,其一是排序方式下降,其二是要求下降且不相等但是用常规的贪心会超时,用二分优化后不超时,注意对l递增排,对w递减排// 类似于POJ 1065,差异有二,其一是排序方式下降,其二是要求下降且不相等// 上面这种解法会超时,用二分优化原创 2010-12-09 21:35:00 · 3342 阅读 · 2 评论 -
POJ 1141 Brackets Sequence 动态规划
在刘汝佳黑书上有详细的解析,自己明天这两种算法自己实现一下,作为对动态规划的深入理解学习先转一个题解http://blog.163.com/leyni@126/blog/static/16223010220103155534476/?fromdm&fromSearch&isFromSearchEngine=yes问题:括号匹配问题。给出一个由括号组成字符串,加最少的括号使之匹配。思路:设f[i][j]为从i到j这段字串达到匹配所需最少括号数。初始状态:f[i][i] = 1;状态转移:f[i][j] = m原创 2010-12-15 23:48:00 · 2085 阅读 · 0 评论 -
POJ 1651 Multiplication Puzzle 动态规划及搜索
这是一道比较简单的DP,通过分析可以设最后拿走的牌为i,则所求的最优解就是i左边和右边子列的最小连乘积再加上x[a]*x[i]*x[b],应为i将原来的序列划分为两个子列,这两个子列符合“最有子结构”和“重叠子问题”的dp特点,他们的最优解互相之间没有影响,只会影响全局问题的最优解,在POJ discuss中的解析比较经典,摘录如下,以后做题可以常常看看discuss,就当学习,但是还是要独立思考为主对于整个牌的序列,最左端和最右端的牌是不能被取走的,除这两张以外的所有牌> ,必然有一张最后取走。取走这原创 2010-12-14 23:35:00 · 2503 阅读 · 0 评论 -
HDU 1176 免费馅饼 动态规划
数塔问题的变形,每一层代表一秒,但要注意下一秒到这一秒,除了左右外还有自己本身这个位置,不能忽略即dp[i][j]=max{dp[i+1][j-1],dp[i+1][j],dp[i+1][j+]}#include using namespace std;int dp[100002][12];int max(int a,int b){ return a>b?a:b;}int main(){ int n,i,j,maxt; int x,t; while(scanf("原创 2010-12-11 21:18:00 · 1533 阅读 · 0 评论 -
HDU 1159 最长公共子序列问题 动态规划
<br />HDU 1159 最长公共子序列问题<br />详见杭电刘春英老师PPT<br /> <br /> <br />//DP算法://用f[i][j] 表示字符串a的第i个字母与字符串b的第j个字母比较,得到两串相比时第i和第j个字母之前相同字母的个数// 如果a[i-1]==b[j-1], f(i,j)= f(i-1,j-1)+1;否则,f(i,j)=max(f(i,j-1),f(i-1,j));#include<iostream>#include<string>using n转载 2010-12-11 20:46:00 · 2272 阅读 · 0 评论 -
POJ1221 计算数的单峰回文分解 动态规划
<br />这题有点难度,主要是考察动态规划,在网上查到一个解答摘录如下,d[pi][j] 表示将 i 分解为其中最大元素为 j 时有多少种分法。<br />还不是很理解,还要多想想。DP真是一个慢慢体会慢慢积累的过程。<br />//计算数的单峰回文分解#include <iostream>using namespace std;int N;typedef unsigned long long ull;ull dp[460][460];int M = 400;void solv原创 2010-12-09 23:09:00 · 2236 阅读 · 6 评论 -
HDU 1466 求直线交点 动态规划
<br />首先自我检讨一下,最近忙一些杂事导致OJ与红宝进度严重滞后,今后要学会专注,回避一些不必要的事情。<br />为了准备12日的校赛做了这题,动态规划,在网上看到的很经典的解法摘录如下:<br /> <br />l 题目分析:<br />将n条直线排成一个序列,直线2和直线1最多只有一个交点,直线3和直线1和直线2最多有两个交点......直线n和其他n-1条直线最多有n-1个交点,由此得出n条直线互不平行且无三线共点的最多交点数:<br />max=1+2+...+(n-1)=n(n-1)/2;原创 2010-12-08 21:46:00 · 1535 阅读 · 0 评论 -
POJ 3624 0-1背包问题 动态规划
第一道背包问题,0-1背包,参考网上一位大牛写的做的。状态方程:dp[i][w] = max{dp[i-1][w], dp[i-1][w-obj[i].wei] + obj[i].val]},但这样会超内存,需要一个空间复杂度的优化将dp改为一维,这招看来以后得常用,具体见转载的《背包九讲》。 明天好好读读《背包九讲》,在多做几道dp变形题,练习在于精不在多。#include using namespace std;const int mMax = 3500;//待选物品个数con原创 2010-11-30 23:57:00 · 2618 阅读 · 0 评论 -
POJ 1015 公正陪审团问题 动态规划
这题想了很久,是经典的动态规划问题,感觉有点难度,在网上看了很多讨论。 看了动态规划还要好好加强。#include using namespace std;int main(){ long i,j,k,l,n,m,t1,maxf,count; long p[210],d[210],f[210],s[210]; long can[30]; long res[30][1000],from[30][1000]; count=0原创 2010-11-27 19:56:00 · 2699 阅读 · 0 评论 -
POJ 1042 钓鱼问题 贪心枚举及动态规划
<br /> <br />题意描述:<br />john现有h个小时的空闲时间,他打算去钓鱼。john钓鱼的地方共有n个湖,所有的湖沿着一条单向路顺序排列(john每在一个湖钓完鱼后,他只能走到下一个湖继续钓), john必须从1号湖开始钓起,但是他可以在任何一个湖结束他此次钓鱼的行程。john在每个湖中每5分钟钓的鱼数(此题中以5分钟作为单位时间),随时间的增长而线性递减。而每个湖中头5分钟可以钓到的鱼数以及每个湖中相邻5分钟钓鱼数的减少量,input中均会给出。并且John从任意一个湖走到它下一个湖的时原创 2010-12-14 18:55:00 · 5926 阅读 · 2 评论 -
POJ 1050 求最大子矩阵和 动态规划
<br />在王晓东算法书上有讲解,是最大子序列和问题的拓广<br />#include <iostream>using namespace std;int rec[102][102];int b[100];int main(){ int n,k,s,sum,max; int i,j; cin>>n; for (i = 0;i < n;i++) for (j = 0;j < n;j++) { cin>> rec[i][j]; } s = 0;原创 2010-12-17 19:17:00 · 1575 阅读 · 0 评论 -
POJ 1088 滑雪问题 动态规划
滑雪问题,用动态规划,计算出每个点作为起始点时的最长,注意计算的中间结果用矩阵保存,最后再所有路径中找出最长那条就是答案,要理解动态规划与递归的区别与联系#include using namespace std;int data[102][102],longetr[102][102];int m,n;int cal(int i,int j){ int max = 0; if (longetr[i][j] > 0) //如果该点已经计算过直接返回路径长度,保存已有的计算结果这是动原创 2010-11-18 21:49:00 · 4141 阅读 · 0 评论 -
POJ 1163 求数字三角形由顶到底边的最大数字和 动态规划
这题是求数字三角形由顶到底边最大数字和对应的路径,在准备夏令营的时候红皮教材上面有,当时没有学动态规划算法,不是很理解,经过这一阵对算法的学习和POJ训练,总算在10分钟内独立思考AC,其实挺简单,满足最优子结构和无后效性,是经典的动态规划问题。一般的思考方法是,由特殊情况比如题目给的示例数据入手,分析如何计算辅助数组dp的值,dp[i][j]记录以r[i][j]为顶点向下走到底边可以得到的最原创 2012-02-11 16:27:43 · 4116 阅读 · 3 评论 -
0-1背包问题与完全背包问题C++实现 动态规划
今天看了看背包九讲,自己写了下0-1背包和完全背包王晓东《计算机算法分析与设计》上面给出的C++实现比较繁琐,相比而言这个版本更加简明给出了测试数据0-1背包问题C++实现/*任务:计算0-1背包问题的最大价值Sample Input10 42 13 34 57 9Sample Output120 1 0 1*/#include#includeint原创 2012-01-03 00:52:58 · 16274 阅读 · 2 评论 -
POJ 2411 大矩形用1X2小矩形填充 状态dp DFS
这题和编程之美上面的“地板覆盖”问题有点像,不同的是,编程之美上面只需要判定能否覆盖,这题需要求出总方案数目结题报告转自 http://duanple.blog.163.com/blog/static/709717672008930104124684/题意:给你一个h*w的矩形,用一个1*2的小矩形去填充,问有多少种填充方法,不考虑对称性。 关键点提示: 1.DFS部分转载 2011-12-24 16:57:26 · 4037 阅读 · 0 评论 -
POJ 1276 取款机零钱组合问题 动态规划
本题为多重背包问题,即每种零钱的个数是有限个,求不超过目标钱数的可以组合出的最大钱数采用DP的思想,先对目标钱数以内的所有面额做逆向遍历,初始dp[0] = true,即0元可凑出在此基础上如果当前的stat可以凑出,那么继续组合出更大的钱数,记下当前有限个零钱和前面的零钱一起可以凑出的不超过目标钱数的所有合法的值。最后从money逆向打印记下的最大的值即可Source Code原创 2011-12-23 13:04:19 · 2433 阅读 · 0 评论 -
背包九讲
背包问题 它是在1978年由Merkel和Hellman提出的。它的主要思路是假定某人拥有大量物品,重量各不同。此人通过秘密地选择一部分物品并将它们放 到背包中来加密消息。背包中的物品中重量是公开的,所有可能的物品也是公开的,但背包中的物品是保密的。附加一定的限制条件,给出重量,而要列出可能的物 品,在计算上是不可实现的。背包问题是熟知的不可计算问题,背包体制以其加密,解密速度快而其人注目。但转载 2011-12-26 10:35:28 · 1488 阅读 · 0 评论 -
POJ 1260 不同等级珍珠组合成最便宜的购买方案 动态规划
这道DP比较简单,要注意题目已经将价格和数量升序排列了,自己不用排序用sum[i]表示前i个等级的珍珠总数那么初始化为前i个等级珍珠一起买,然后逐步计算前0、1 ...... i-1等级分开买的价钱,取最小即可状态转移方程为 dp[i] = min(dp[i], ((sum[i] - sum[j]) + 10) * p[i] + dp[j]); 用一个二重嵌套循环就搞定了Sourc原创 2011-12-23 01:19:15 · 2226 阅读 · 0 评论 -
POJ 1080 基因序列相似度计算 动态规划
本题为典型的动态规划,关键找出序列比对的3个不同情况,即子问题设d[i][j]为取s1第i个字符,s2第j个字符时的最大分值则决定p为最优的情况有三种 p数组为分数矩阵1、 s1取第i个字母,s2取“ - ”: d[i-1][j]+p[ s1[i-1] ]['-'];2、 s1取“ - ”,s2取第j个字母:d[i][j-1]+p['-'][ s2[j-1] ];3、 s原创 2011-12-23 00:11:04 · 3825 阅读 · 0 评论