This is a creation in Article, where the information may have evolved or changed.
First issue:
- Delete Operation for Strings
Given words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each Step can delete one character in either string.
To be blunt is to find two string non-contiguous maximum common string. If the DP algorithm is very familiar with the solution of the problem can be quickly thought of, but I am not very familiar with, so with a very slow way, personal understanding should be divided into the law, many steps have been repeated many times.
The writing is very rubbing, light spray.
The following is to introduce a simple and easy to understand DP algorithm, first on the code (Leetcode wrote, I just rewrite it with Golang)
The idea is simple, like word1=abcd,word2=obdce.
Save the computed value with a two-dimensional array (add a line in the code and a column 0 for easy calculation)
For example, when comparing Word1 C and Word2 C, the maximum of AB and OBD is 1, so this position requires only dp[i-1][j-1]+1
Dynamic planning and divide-and-conquer differences:
Dynamic planning: It is often used to solve problems with some of the best properties. There may be many possible solutions to this type of problem. Each solution corresponds to a value, and we want to find the solution with the best value. The dynamic programming algorithm is similar to the partition method, and its basic idea is to decompose the problem into several sub-problems, solve the problem first, then get the solution of the original problem from the solution of these sub-problems. Different from the partition method, the problem which is suitable for solving with the dynamic programming is not independent of each other by decomposition.
Divide-and-conquer method: If the division of the method to solve such problems, the decomposition of the number of sub-problems are too many, some sub-problems have been repeated calculation of many times. If we can save the answers to the solved sub-problems and find out the answers when we need them, we can avoid a lot of repetitive calculations and save time. We can use a table to record the answers to all the solved sub-problems.
Note: Regardless of whether the sub-problem is used later, as long as it is calculated, the results are filled into the table. This is the basic idea of the dynamic programming method.
This is a question that allows me to have a deeper impression of dynamic planning, so record it. Although the former will write, but every time encountered problems will not be unexpected to use, or their own neglect of practice.
The paper came to the end of shallow, I know this matter to preach ah.