DP Editing Distance problem

Source: Internet
Author: User
Tags first string

Test instructions

The editing distance problem is to give two strings;
Such as:
ABCdef
Abbefc
The second string becomes the first string by adding, deleting, and changing three ways;
It is obvious that this example is a 3-time change;
The minimum number of changes to a given string after the topic is asked;

Understand:

A look at this problem is actually covered;
Of course there is a way to do, that is, wide search;
But the wide search in time is not solve the problem;
So we have to seek another way out;
The real concrete problem-solving idea is as follows:
First of all:
We add some underscores to two strings;
The
Abcdef_
Ab_befc
Can be seen:
By adding or deleting changes can obviously transform the past;
So what is the case, what is changed, and what is the deletion of it?
We now have a minimum value f (i, j) that needs to be changed;
represents the smallest change that exists between the I of the first string and the J character of the second string;
We know if str1[i] = = Str2[j] words;
Then: F (i, j) = f (i-1, j-1);
if not equal;
Then: F (i, j) = f (i-1, j-1) + 1;
This is the change;
And the first I character is the same as the former j-1 characters, then it is necessary to delete;
Such as:
Abab
Ababc
Then: F (i, j) = f (i, j-1) + 1;
And the first i-1 characters in the first J characters are the same, then add;
Ababa
Abab
Then: F (i, j) = f (i-1, J) + 1;
The last two are difficult to understand, but indispensable;
Thus, we can find a minimum value by the above three formulas;
The answer is the minimum value;

The code is as follows:

#include <cstdio> #include <cstring> #include <cmath> #include <ctime> #include <iostream > #include <algorithm> #include <vector> #include <string> #include <map> #include <set

> #include <queue> #include <stack> using namespace std;
typedef long Long LL;

typedef pair<int, int> PII;
Const double Min_inf = 1e-7;

const int max_inf = (1e9) + 7;
    #define X First #define Y second int main () {string str1, str2;
    CIN >> str1 >> str2;

    int len1 = Str1.length (), len2 = Str2.length ();
    Vector<vector<int> > DP (LEN1 + 2, vector<int> (len2 + 2, 0));
    for (int i = 1; I <= len1; ++i) {//initial value dp[i][0] = i;
    } for (int i = 1; I <= len2; ++i) {//initial value dp[0][i] = i; } for (int i = 1, i <= len1; ++i) {for (int j = 1; j <= Len2; ++j) {if (str1[i-1] = = St
   R2[j-1]) {dp[i][j] = dp[i-1][j-1];         } else {Dp[i][j] = min (dp[i-1][j-1], min (Dp[i-1][j], dp[i][j-1])) + 1;

    }}} cout << dp[len1][len2] << Endl;
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.