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的更多相关文章

  1. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  3. 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 ...

  4. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  5. poj2385 简单DP

    J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  6. hdu1087 简单DP

    I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     ...

  7. poj 1157 LITTLE SHOP_简单dp

    题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...

  8. 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 ...

  9. Codeforces 41D Pawn 简单dp

    题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...

随机推荐

  1. BZOJ2134——单选错位

    1.题意:这就是说考试的时候抄串了一位能对几个(雾) 2.分析:这是一个期望问题,期望就是平均,E(a+b)=E(a)+E(b),所以我们直接算出每个点能对几个就好,那么就是1/max(a[i],a[ ...

  2. Lodash.js的库

    1.orderBy _order(数组,排序对象,["asc"]升序或者["desc"]降序)

  3. tengine/nginx-tomcat动静分离遇到的问题

    小站安装好tengine后,接下来的工作就是要配置好tengine让其和后端的tomcat正常的连接工作起来,tengine的配置文件本身比较简单,网上有大量的相关介绍说明文档,我这里只是摘出我配置过 ...

  4. 推荐一篇 关于REST 和 SOAP区别的文章

    写的很出色! https://www.ibm.com/developerworks/cn/webservices/0907_rest_soap/ 我的感觉就是REST针对的是资源,通过api的URL就 ...

  5. phpstudy 80端口被占用,修改端口

    搭建mantis,总会出现80端口被占用的情况.看到别的步骤是:1.cmd 运行netstat -ano查看80端口被什么占用,然后在任务管理器找到对应的结束进程.通常情况下是被System占用,右击 ...

  6. Firefox Portable Developer 52.0.0.6176-6178

    FirefoxPortableDeveloper-52.0.0.6176.7z 47.9 MB FirefoxPortableDeveloper-52.0.0.6178.7z 55.8 MB

  7. 带蒙版的提交loading页面实现

    废话不多说,直接上代码. 首先是HTML中层的实现: <!-- loading 层 --> <div id="loadingDivBack" style=&quo ...

  8. Hexo博客建设

    上次我教大家利用wordpress程序搭建个人网站,这次换个Hexo来搭建咱们的个人博客,那咱们开始吧!先提供官网给大家英文版的请点击这里,中文版的请点击这里,好了然后是工具要准备下,貌似就只要安装g ...

  9. python之路:Day01 --- Python基础1

    本节内容 1.Python介绍 2.发展史 3.变量 4.用户输入 5.表达式 if...else语句 6.表达式 for 循环 7.表达式 while 循环 8.模块初识 9.数据类型初识 10.数 ...

  10. JS监听输入框值变化兼容 onpropertychange、oninput

    onpropertychange 属IE oninput 属除IE外(Chrome.Firefox.SS) 所以肯简单的办法嘛: 1. 一个input里面写两个属性事件 2.写在JS中判断浏览器添加监 ...