The longest common sub-sequence and editing distance of dynamic programming

Source: Internet
Author: User
Tags min strlen
1. What is dynamic planning

Dynamic programming (programming) is a branch of operations research and a mathematical method for the optimization of decision-making processes (decision process). The famous optimization principle (principle of optimality) was presented in the early 1950s when the American mathematician R.e.bellman and others studied the optimization of the multi-stage decision-making process (multistep decision process). In this paper, the multi-stage process is transformed into a series of single-stage problems, and the relationship between the stages is solved, and a new method to solve the problem of the process optimization is created, which is dynamic programming. 1957 published his masterpiece The Dynamic programming, which is the first book in the field.
2. Scope of application of dynamic planning

Dynamic planning is typically applied to optimization problems. There are a number of possible solutions to such problems, but we need to find an optimal solution that fits our criteria. Such problems can usually be solved by decomposing into sub-problems. First, dynamic programming solves the problem by combining the solution of the sub-problem. This is somewhat similar to the Division, but the division is divided into independent sub-problems, while the dynamic programming for sub-problem is not independent situation, that is, each sub-problem contains the same sub-sub-problem. The dynamic programming algorithm solves each sub-sub-problem only once, saves the result, and can be applied directly to the next encounter without having to repeat the calculation.3. Features of dynamic planning (1) Optimal substructureIf a solution of the problem contains the optimal solution of a sub-problem, the problem has an optimal substructure.(2) Overlapping sub-problemsThe second characteristic is that the sub-problems we get when we decompose the original problem must be the same, rather than creating new sub-problems, such as the separation and treatment, which constantly produce new sub-problems when applied. The dynamic programming algorithm always makes full use of the sub-problem is the same feature, each sub-problem is solved only once, that is, the first time encountered when the solution, save it in a table, and later in the use of direct query access.4. Two examples of dynamic planning (1) The longest common subsequenceProblem Description: A sequence s, if it is a subsequence of two or more known sequences, and is the longest of all conforming to this condition sequence, then S is called the longest common subsequence of the known sequence.
First, we define X (i) as the string of the first I character of the x, and Y (j) as the string of the first J characters of the Y, and it is clear that Y (0) is an empty string. Z (k) is the longest common sub-sequence of x (i) and Y (j). Then if the last character of X (i) x (i) equals the last character Y (j) of Y (j), then Z (k-1) must also be the longest common subsequence of X (i-1) and Y (j-1). This is easy to get through contradiction. This shows the optimal sub-structure, and the optimal solution of the problem contains the optimal solution of the sub-problem. At the same time we also see that there are many sub-sub-problems are the same, we need to solve the problem. Make C (i) (j) The common subsequence length of x (i) and Y (j). (a) if X (i) =y (j), then C (i) (j) =c (i-1) (j-1); (b) if x (i). =y (j), then C (i) (j) =max (C (i-1) (j), C (i) (j-1)); The solution of the original problem is C (len1) (Len2), and Len1, len2 are the length of the string, respectively, by the state transfer equation above.(2) editing distanceProblem Description: Editing distance (edit Distance), also known as Levenshtein distance, refers to the minimum number of editing operations required between two strings, from one to another. Permission edits include replacing one character with another character, inserting a character, and deleting a character. For example, to convert the word kitten to Sitting:sitten (k→s) sittin (e→i) sitting (→g), we define C[I][J] as the number of operation steps for string x (i) to String Y (j). Similarly, we consider if (a).      The last character of X (i) x (i) ==y (j) of the last character Y (j) so c[i][j]=c[i-1][j-1]; Because by definition we know that c[i-1][j-1] is the number of edits to string X (i-1) to character y (j-1), the last character is equal to X (i) ==y (j), then the last character does not need to be edited to finish editing the string x (i) to Y (j). (b). The last character of X (i). =y (j) The last character Y (j) is also divided into three cases (1) if the last character x (i) needs to be replaced by Y (j), then c[i][j]=c[i-1][j-1]+1;    (2) If the last character x (i) needs to be deleted, then c[i][j]=c[i-1][j]+1; Because by definition, c[i-1][j] is the number of operations required to convert a string x (i-1) to Y (j), that is, if the string X (I-1) has been successfully converted to Y (j), then it is clear that the last character X (i) needs to be deleted (3) if the last character x (i) is required After adding a character that is Y (j), then c[i][j]=c[i][j-1]+1; Because the definition knows, c[i][j-1] is the number of operations required to convert the string x (i) to Y (j-1), and to convert to Y (j), you can add only one step to the added character, that is, the insertion character y (j) in summary, C[i][j]=min (c[i-1][j-1],c[ I-1][J],C[I][J-1]) +1;
CSDN Programming Challenge Module A simple version of the editing distance problem http://student.csdn.net/mcs/programming_challenges
The description is as follows: the traditional editing distance there are three kinds of operations, namely, add, delete, change, we now have to discuss the editing distance allows only two operations, that is, add one character, delete one character. We ask this editing distance of two strings, which is the minimum number of operations to turn a string into another string. Input format: Multiple sets of data, two rows per set of data, one string per line. Each string is not more than 1000 in length and is composed of uppercase English letters. Output format: Each set of data output lines contains an integer representing the number of times the minimum action is required. Through the code is as follows:

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
int min (int a,int b)
{
    return A<b?a : b;
}
int c[1030][1030];
Char stra[10130],strb[1030];
int main (int argc, const char * argv[])
{
    while (scanf ("%s", Stra)!=eof)
    {
        scanf ("%s", STRB);
        
        int Len1=strlen (stra);
        int Len2=strlen (STRB);
        for (int i=0; i<=len1; i++)
        {
            c[0][i]=i;
        }
        for (int j=0; j<=len2; j + +)
        {
            c[j][0]=j;
        }
        for (int i=1, i<=len2;i++) for
            (int j=1; j<=len1; j + +)
            {
                if (strb[i-1]==stra[j-1])
                {
                    c[ I][J]=C[I-1][J-1];
                }
                else
                {
                    c[i][j]=min (c[i-1][j], c[i][j-1]) +1;
                }
            }
        printf ("%d\n", C[len2][len1]);
    }
    return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.