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. 【新手出发】从搭虚拟机开始,一步一步在CentOS上跑起来.Net Core程序

    文章背景 微软6月26号发布core 1.0版本后,园子里关于这方面的文章就更加火爆了,不管是从文章数量还是大家互动的热情来看,绝对是最热门的技术NO.1.我从去年底开始接触.net core到现在也 ...

  2. Anti XSS 防跨站脚本攻击库

    https://wpl.codeplex.com/ Before understanding Anti-Cross Site Scripting Library (AntiXSS), let us u ...

  3. Excel2013 基本用法(下)

    排序 按行/列排序 选择单元格,点击"排序"(点击之后自动全选) 点击选项,弹出"排序选项",根据自己需要选择按行排序/按列排序. 输入排序条件. 确定. 复杂 ...

  4. LruCache算法原理及实现

    LruCache算法原理及实现 LruCache算法原理 LRU为Least Recently Used的缩写,意思也就是近期最少使用算法.LruCache将LinkedHashMap的顺序设置为LR ...

  5. (三)Netty源码学习笔记之boss线程处理流程

    尊重原创,转载注明出处,原文地址:http://www.cnblogs.com/cishengchongyan/p/6160194.html  本文我们将先从NioEventLoop开始来学习服务端的 ...

  6. windows耳机没有声音

    问题描述: 扬声器有声音,耳机没有声音,重装了驱动也没有用,系统配置.耳机也没有问题,具体原因我不太清楚,参考网址:http://forum.ubuntu.org.cn/viewtopic.php?f ...

  7. HTML 5 背离贪吃蛇 写成了类似于屏幕校准

    中间写了改 改了写 还是没做出自己满意的效果 ,看来自己的确不是一个走前端的料子.当然h5还是学一点好一点 具体说来 就是 在canvas 的画布中 鼠标点击后画上一个圆形 然后就有随机的在画布上面出 ...

  8. nodeJS常用的定时执行任务的插件

    later:https://github.com/bunkat/later star:1765 fork:120 node-schedule  https://github.com/node-sche ...

  9. Java_File类讲解_打印目录树状结构_递归算法

    package cn.xiaocangtian.testFile; import java.io.File; public class FileTree { public static void ma ...

  10. iOS 字符串删除 DOM

    iOS  string 删除 包含的 DOM NSMutableString *mutableString = [NSMutableString stringWithString:responseSt ...