四道简单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 每一个格子仅仅 ...
随机推荐
- [每日一记] Python报错 综述
提纲 -- Syntax errors -- Static semantic errors -- Full semantic errors -- 使用一门语言,不论是自然语言还是编程语言,我们需要注意 ...
- 理解 Delphi 的类(八) - 关于类的定义
//标准语法 TMyClass1 = class(TObject) end; //如果是继承自 TObject 可以省略 TMyClass2 = class end; // ...
- RBAC中 permission , role, rule 的理解
Role Based Access Control (RBAC)——基于角色的权限控制 permission e.g. creating posts, updating posts role A ro ...
- CSS的5种常用的垂直居中的方法
1.绝对定位上下百分之五十然后上外边距做外边距都是他的宽高的一半 #child{ width: 200px; height: 150px; position: absolute; left: 50%; ...
- 各种Android手机Root方法
Root的介绍 谷歌的android系统管理员用户就叫做root,该帐户拥有整个系统至高无上的权利,它可以访问和修改你手机几乎所有的文件,只有root才具备最高级别的管理权限.我们root手机的过程 ...
- Java实现线性阈值模型(Linear Threshold Model)
影响力传播的线性阈值模型: 网络中连接任意两个节点u,v之间的边都有权重,任意一个节点它的各个邻居节点的边的权重之和为1,即 N(v):neighbors of v. 网络中的节点分为已激活节点和未激 ...
- C# WinForm实现Windows 7 Aero磨砂玻璃效果
在Vista系统之后,微软为窗体程序提供了Aero磨砂的效果,如下图.那么用C#如 何来实现这种磨砂效果呢? 代码: using System; using System.Collections.Ge ...
- Camtasia 录屏说明
准备好要录制的屏幕或网页,在即将播放的位置暂停住. 从开始菜单位置“TechSmith”启动Camtasia Recorder 8,其界面如下所示: 注意,要录制系统声音,须在Recorded inp ...
- Python 30分钟入门——数据类型 and 控制结构
Python是一门脚本语言,我也久闻大名,但正真系统的接触学习是在去年(2013)年底到今年(2014)年初的时候.不得不说的是Python的官方文档相当齐全,如果你是在Windows上学习Pytho ...
- 2048游戏_QT实现
#ifndef GAMEWIDGET_H #define GAMEWIDGET_H #include <QWidget> #include <QMouseEvent> #inc ...