四道简单DP
DP类题目找到子问题(状态),然后找到转移方程,就OK
#dp
#likes matrixchain
#according to two point's distance to recurrence
class Solution:
# @return a string
def longestPalindrome(self, s):
length = len(s)
p = [[0 for col in range(length)] for row in range(length)]
for i in range(len(s)):
p[i][i] = 1
if(i<(length-1) and s[i] == s[i+1]):
maxlenth = 2
p[i][i+1] = 1
start = i
for i in range(3,length+1):
for j in range(length-i+1):
k = j + i - 1
if(s[j]==s[k] and p[j+1][k-1] == 1):
maxlenth = i
p[j][k] = 1
start = j
return s[start:start+maxlenth] if __name__ == '__main__':
s = Solution()
test_str = 'abcba'
print s.longestPalindrome(test_str)
#dp least number coins
#one:overlapping subproblem
#two:optimal substructure
class Solution:
# @return a string
def least_number_coin(self, s, v):
#first initialization
min_number = [1000000]*(s+1)
min_number[0] = 0
#And then,recurrence
#time need O(n^2),and additional time O(n) to save the subproblem's temp solution
for i in range(1,s+1):
for j in range(len(v)):
print i,v[j]
if(v[j]<=i and (min_number[i-v[j]]+1 < min_number[i])):
min_number[i] = min_number[i-v[j]]+1
print min_number
return min_number[s]
if __name__ == '__main__':
s = Solution()
money = 11
v = [1,3,5]
print s.least_number_coin(money,v)
#dp
#time O(n^2),addtional O(n) to save temp result
class Solution:
# @return a string
def LIS(self,v):
print v
d = [1]*(len(v))
print d
for i in range(len(v)):
for j in range(i):
print i,j
if (v[j]<=v[i] and d[j]+1>d[i]):
d[i] = d[j]+1
print d
print max(d)
if __name__ == '__main__':
s = Solution()
v = [5,3,4,8,6,7]
s.LIS(v)
#dp
class Solution:
# @return a string
def max_number_apples(self, apples, n, m):
print apples
s = [[0 for col in range(m)] for row in range(n)]
for i in range(n):
for j in range(m):
if(i==0 and j==0):
s[i][j] = apples[0][0]
elif(i==0 and j>0):
s[i][j] = s[i][j-1]+apples[i][j]
elif(j==0 and i>0):
s[i][j] = s[i-1][j] + apples[i][j]
else:
if(s[i-1][j]>s[i][j-1]):
s[i][j] = s[i-1][j] + apples[i][j]
else:
s[i][j] = s[i][j-1] + apples[i][j]
print s
return s[n-1][m-1]
if __name__ == '__main__':
s = Solution()
n = 3
m = 4
apples = [[0 for col in range(m)] for row in range(n)]
k = 1
for i in range(n):
for j in range(m):
apples[i][j] = k
k = k + 1
s.max_number_apples(apples,n,m)
one:initialization(初始化)
two:recurrence(递推)
多项式时间,需要额外的存储空间
四道简单DP的更多相关文章
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- codeforces Gym 100500H A. Potion of Immortality 简单DP
Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...
- 简单dp --- HDU1248寒冰王座
题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...
- poj2385 简单DP
J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- hdu1087 简单DP
I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB ...
- poj 1157 LITTLE SHOP_简单dp
题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...
- hdu 2471 简单DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2571 简单dp, dp[n][m] +=( dp[n-1][m],dp[n][m-1],d[i][k ...
- Codeforces 41D Pawn 简单dp
题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...
随机推荐
- CPUID指令简单调用
关于CPUID指令,可以看维基百科的相关介绍 https://en.wikipedia.org/wiki/CPUID 在windows下可以调用__cpuid和__cpuidex这两个函数,__cpu ...
- AlloyTouch插件
1.老样子引入js <script src="js/transform.js"></script> <script src="js/allo ...
- Code First 迁移,及迁移错误
迁移错误: 今天在使用EF6 Code First时,出现如下错误,折腾了老半天.分享一下,帮后面的兄弟少走弯路. PM> Enable-Migrations Checking if the c ...
- 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]
[本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...
- FreeCodeCamp心得
<img> <input> tags are self-closing. So that there is only one tag without a slash i ...
- cxf WebService设置wsdl中soapAction的值
用cxf开发一个WebService很简单,只需要下面几步: 1.定义接口 public interface HelloService { String hello(); } 2.实现 public ...
- Linux 软件包管理
简介: linux中软件包的管理随着linux版本的不同而不同,一般RPM和DPKG是最常见的两类软件包管理工具.分别应用基于rpm软件包的linux发行版本和基于deb软件包的linux发行版本. ...
- eclipse maven maven-archetype-webapp 创建失败
如果在eclipse中发现创建maven失败,大部分的原因是因为本地仓库坏了,或是少东西了,最直接的方法就时删掉重新下载就好了
- [Android Pro] Android开发实践:自定义ViewGroup的onLayout()分析
reference to : http://www.linuxidc.com/Linux/2014-12/110165.htm 前一篇文章主要讲了自定义View为什么要重载onMeasure()方法( ...
- 模拟搭建Web项目的真实运行环境(四)
本篇介绍如何部署mongodb环境,主要分为三个部分: 第一部分 介绍如何在ubuntu下安装mongodb, 第二部分 介绍如何在windows下安装使用MongoChef客户端, 第三部分 介绍在 ...