knapsack problem

Now go to one that can hold 4 How to pack a backpack with the highest value per unit weight :A weight 1 A unit of , value 15;B weight 3 A unit of , value 20;C weight 4 A weight , value 30

Use dynamic programming to fill in spaces

class SolutionBag:
def valuableBag(self,optionalList,sizeBig):
# Create grid
grid = [[0 for i in range(sizeBig+1)] for j in range(len(optionalList)+1)]
# From row / column serial number 1 Start counting
column = 1
for v in optionalList.values():
optionalWeight,optionalPrice = v
for row in range(sizeBig):
if optionalWeight > row+1:
grid[column][row+1] = grid[column-1][row+1]
grid[column][row+1] = max(grid[column-1][row+1],optionalPrice+grid[column-1][row+1-optionalWeight])
column += 1 return grid

The longest public substring

In dynamic planning , You have to maximize an indicator . In this case , You have to find the longest common substring of two words .fish and fosh What is the longest substring contained in all

How to divide this problem into subproblems ? You may need to compare substrings : It's not a comparison hish and fish, But compare first his and fis

We use grid filling method to realize

# Pseudo code 

# If the letters are the same, the upper left +1
if word1[i] == word2[j] :
cell[i][j] = cell[i-1][j-1] +1
cell[i][j] = max(cell[i][j-1],cell[i-1][j])

python Implementation grid

class SolutionLengthS:
def longestLength(self,str1,str2):
grid = [[0 for j in range(len(str2)+1)] for i in range(len(str1)+1)]
for i in range(len(str2)):
for j in range(len(str1)):
if str1[j] == str2[i] :
grid[i+1][j+1] = grid[i][j] + 1
grid[i+1][j+1] = max(grid[i+1][j],grid[i][j+1])
return grid

python Dynamic programming ( Knapsack problem and longest common substring ) More articles about

  1. Dynamic programming & character string : The longest public substring

    Or directly up the transfer equation : Dynamic gauge can only solve O(n^2) The longest common substring problem of Use suffix array or SAM It can solve this problem efficiently therefore , For this question , The code of the moving gauge is not given Give directly SAM The implementation of the , Also for future study SAM Hit the next ...

  2. Dynamic programming 1—— The longest increasing subsequence 、 Longest common subsequence 、 The longest public substring (python Realization )

    Catalog 1. The longest increasing sequence 2. Longest common subsequence 3. The longest public substring 1. The longest increasing sequence Given a sequence , Find the longest , The length of strictly increasing subsequences ( No continuity is required ). Solution 1 : Dynamic programming Record each... Through an auxiliary array ...

  3. [Python] Longest common subsequence VS The longest public substring [ Dynamic programming ]

    Preface Because the original Microsoft open source is based on the ancient perl Linguistic Rouge The dependent environment is really difficult to build , Then follow Rouge The description of the paper is realized by itself . Rouge There is N.L.S.W.SU Wait for several sub evaluation indicators . It's happening again Rouge-L Function time , then ...

  4. use Python Calculate the longest common subsequence and the longest common subsequence

    How to use Python Calculate the longest common subsequence and the longest common subsequence 1. What is the longest common subsequence ? What is the longest common substring ? 1.1. Longest common subsequence (Longest-Common-Subsequences,LCS) Longest male ...

  5. Dynamic programming ( One )—— The longest common subsequence and the longest common substring

    notes : The longest common subsequence is solved by dynamic programming , Because the subproblems overlap , So we use array to cache the result , Save the best value direction . When outputting the results , Then build a binary tree from top to bottom , Bottom up output , There is no fork in the process , Result only . The longest common substring is a reference string ...

  6. 《 Introduction to algorithms 》 Dynamic planning of reading notes — Longest common subsequence & The longest public substring (LCS)

    From: 1. The longest common subsequence under schenkorp & The difference between the longest common substring : Find the longest common substring of two strings , This string is going to ...

  7. Dynamic programming classic —— The longest common subsequence problem (LCS) And the longest common substring problem

    One . The longest common subsequence problem (LCS problem ) Given two strings A and B, The lengths are m and n, Ask to find their longest common subsequence , And return its length . for example : A = "HelloWorld"    B = & ...

  8. 【ZH cheese 】 How to use Python Calculate the longest common subsequence and the longest common subsequence

    1. What is the longest common subsequence ? What is the longest common substring ? 1.1. Longest common subsequence (Longest-Common-Subsequences,LCS) Longest common subsequence (Longest-Common-Subseq ...

  9. Dynamic programming —— Longest common subsequence && The longest public substring

       Longest common subsequence (LCS) It's a typical problem of verb return .  problem Given two sequences ( A sequence of integers or strings )A and B, A subsequence of a sequence is defined as a new sequence obtained by taking several elements out of the sequence according to the monotonic increasing order of the index , For example, from the sequence A Remove from A ...

  10. [ Programmer code interview guide ] Recursion and dynamic programming - The longest common substring problem (DP,LCST)

    Problem description As the title . example : Enter two strings str1="1AB234",str2="1234EF" , The longest common substring should be output "234". Their thinking shape ...

Random recommendation

  1. CSS Weight and style priority

    CSS Weight value calculation The overall weight value of a style rule consists of four independent parts :[A, B, C, D]; (1) A Represents the inline style ( On the label style Properties of the ), Only 1 perhaps 0 Two values : For inline styles , Because there is no choice ...

  2. python Practice the program (c100 Classic example 9)

    subject : Request to output chess board . for i in range(1,9): for j in range(1,9): if i%2==0: if j%2==0: print '*', else: pri ...

  3. Use foreach Problems encountered

    foreach($list as $k=>$v ){ ........... } And the following : foreach($list as &$v){ ........ } In fact, the results of the two are the same , but ...

  4. sqlsever About index

    Indexes : stay sqlserver in , The minimum unit of storage is page , The pages are inseparable B Trees : The original intention is to reduce the number of disk scans , If a table or index is not used B Trees ( For tables without clustered indexes, use Heap Heap for storage ), So find a number ...

  5. [MODx] 10. Using Babel for Muti-languages support

    1. Go to 'Extras' -> download and install 'Babel'. 2. Set up '.htaccess' file, currently, we set ...

  6. turn :Struts2 Frame security flaws

    At present java Develop web site , Usually not pure JSP Of , Most of them use java framework. With these framework, Let developers develop code more quickly , It also makes the code very extensible , The idea of layered architecture , More in-depth ...

  7. Java Understanding stage of design pattern

    What is the design pattern ? Design patterns (Design pattern) It's a set of reusable . Most people know that . Catalogued . Summary of code design experience . Its essence is the combination of inheritance and interface . Why use design mold ? Design patterns are used to ...

  8. Leetcode 181. Employees Earning More Than Their Managers

    The Employee table holds all employees including their managers. Every employee has an Id, and there ...

  9. 1.Java The first lesson : First time to know java

    Today is also a formal start to learn Java 了 , I don't learn too much in one day , To get started Java. Fortunately, what I'm learning now is basic , Can keep up with the schedule , I hope I can keep this state of mind and keep learning . Let's talk about what we learned today . 1 Computer ...

  10. English study APP case analysis

    The first part research , evaluating 1. Hands on experience Interface simple , It has regular dictionary translation function , There are special requirements for the students of CET-4 or CET-6 " Word challenge " modular , And for oral training " I love speaking English " modular , multi-function ...