Dynamic Programming classic example - edit distance problem

Problem Description:

  Two strings, a string that is the starting point, and the other is the end.

  For example, the end of the string to the beginning of the string ddl de conversion steps:

  ddl->del->def。

  Edit distance of 2.

Analysis of Algorithms:

  Ddl first consider the example above the first character and the first character def, and that they are the same, so only need to calculate a [2 ... lengthA] and b [2 ... lengthB] (dl and ef the distance between) can be.

  If the first character of two different strings, and DEF adl e.g., can be one of three:

  • Before the end point of the first character string is inserted into the starting point of the first character string (adl-> dadl), then calculates a [1 ... lengthA] and b [2 ... lengthB] (dadl and DEF) of distance can;
  • Deleting the first character string starting point (adl-> dl), and then calculate a [2 ... lengthA] and b [1 ... lengthB] (dl-> def) to a distance;
  • The first character string as a starting point to modify the end of a first character string (adl-> ddl), and then calculates the distance a [2 ... lengthA] and b [2 ... lengthB] (dl and ef), i.e. can.

  Consider starting point string of the i-th character string and the end of the j-th character of words is the same.

When not considering starting string before i-1 character and end string before the j-1 th character, if the starting string of the i-th character and the end of the string of the j-th character are equal, i.e., a [i-1] = b [ j-1], only need to compute a [i ... lengthA] and the distance between b [j ... lengthB] to.

  If not equal, you can be one of three:

  • Before the end point of the j-th character string inserted to the beginning of the i-th character string, and then calculates a [i ... lengthA] and b [j + 1 ... lengthB] to distance;
  • Deletion start point of the i-th character string, and then calculates a [i + 1 ... lengthA] and b [j ... lengthB] to distance;
  • String edit start point to the end of the i-th character in the j-th character string, and then calculates the a [i + 1 ... lengthA] and b [j + 1 ... lengthB] to distance;

     So what is the basis for one of three is it? Is the starting point i-1 before the string and end character string edit distance before j-1 characters.

  Recursive equation:

 

 

 

  edit [i-1] [j] +1 corresponds to the starting end of the string is inserted the end of the last character string, so that the insert edit + 1, after calculating the edit [i-1] [j];

  edit [i] [j-1] +1 corresponds to the starting point of the last character string delete, delete edit + 1, after calculating the edit [i] [j-1];

  edit [i-1] [j-1] + flag corresponds by replacing the last character string starting end to the last character string. If a [i-1] = b [j-1], flag = 0;! If a [i-1] = b [j-1], flag = 1.

  

#include<iostream>
#include<string.h>
using namespace std;


int main() {
    char a[2000], b[2000];

    cin.getline((a+1), 2001);
    cin.getline((b+1), 2001);
    
    int lengthA = strlen(a) - 1;
    int lengthB = strlen(b) - 1 ;
    
    int editDistance[2001][2001];
    editDistance[0][0] = 0;
    
    for(int i = 1; i <= lengthA; i++) {
        editDistance[i][0] = i;
    }
    for(int i = 1; i <= lengthB; i++) {
        editDistance[0][i] = i;
    }
    for(int i = 1; i <= lengthA; i++)
        for(int j = 1; j <= lengthB; j++) {
            if(a[i] == b[j]) {
                editDistance[i][j] = editDistance[i-1][j-1];
            }
            else {
                editDistance[i][j] = min(editDistance[i-1][j], min(editDistance[i][j-1], editDistance[i-1][j-1])) + 1;
            }
        }
    
    cout << editDistance[lengthA][lengthB];
    
    return 0;
}
View Code

Complexity Analysis

  Time complexity: O (m * n)

  Space complexity: O (1)

Experience

It is important recurrence equation.

Guess you like

Origin www.cnblogs.com/Texas/p/11717255.html