Leetcode:edit Distance

Source: Internet
Author: User

Title:

Given words word1 and word2, find the minimum number of steps required to convert word1 to Word2. (Each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

A) Insert a character
b) Delete a character
c) Replace a character

This problem can still be used in dynamic programming, the question is how to get the transfer equation.

If a[i] = = B[j] then d[i,j] = d[i-1,j-1]

But how do you calculate it when it's not equal?

There are three possible ways in which the results are based on these three ways

If a[i]! = b[j] Then min (

a[i-1,j]//equivalent to delete a[i-1]

The a[i,j-1]//is equivalent to inserting a

a[i-1,j-1]//equivalent to replacement

)

intMindistance (stringWord1,stringWord2) {        intm =word1.size (); intn =word2.size (); Vector<vector<int> > Result (m+1,vector<int> (n+1));  for(inti =0; I <= m; i++) result[i][0] =i;  for(intj =0; J <= N; J + +) result[0][J] =J;  for(inti =0; I < m; i++){             for(intj =0; J < N; J + +){                if(Word1[i] = =Word2[j]) result[i+1][j+1] =Result[i][j]; ElseResult[i+1][j+1] = min (result[i][j+1],min (result[i+1][J],RESULT[I][J]) +1; }        }        returnResult[m][n]; }

Other Related questions:

(1)

Longest common string (continuous)

String a= "abcdef";

String b = "Abdef";

You can use dynamic planning to solve, using a two-dimensional array, state d[i,j] to represent the longest common string to a[i] and b[j], so the problem is to find the state transition equation.

If a[i] = B[j] Then, d[i,j] = d[i-1,j-1]+1

If a[i]! = B[j] Then, d[i,j] = 0

Finally, iterate over the array to find the largest string.

optimization, first of all, the step to find the maximum string can be put into the calculation process.

stringLCS (stringS1,stringS2) {        intLen1 =s1.length (); intLen2 =s2.length (); intMaxLength =0; intindex =0; inttable[1005][1005];  for(inti =1; I < len1+1; i++) table[i][0] =0;  for(inti =1; I < len2+1; i++) table[0][i] =0;  for(inti =1; I <= len1; i++){             for(intj =1; J <= Len2; J + +){                if(s1[i-1] = = s2[j-1]) {Table[i][j]= table[i-1][j-1] +1; }Else{Table[i][j]=0; //Table[i][j] = (Table[i-1][j] > table[i][j-1])? Table[i-1][j]: table[i][j-1];                }                if(Table[i][j] >maxLength) {MaxLength=Table[i][j]; Index=i; }            }        }        returnS1.substr (index-maxlength,maxlength);}

Exceptions, the computational space of the general dynamic programming can be reduced. The two-dimensional space is reduced to one-dimensional space.

dimensionality reduction for j is generally positive and reverse, the key is to see, if in the calculation process j-1 will be calculated in advance, it is to be in reverse order. For example, the state shift is

TABLE[I][J] = table[i-1][j-1] + 1;
If J is from 0 to Len2, then table[j-1] will be calculated first, but from state transfer we know that the table[j-1 of this line should be in the calculation of Table[j] is still on the previous line, so it should be reversed.
stringLcs_continue (stringS1,stringS2) {    intLen1 =s1.size (); intLen2 =s2.size (); Vector<int> Result (len2+1); intLongest =0; intindex =0;  for(inti =0; I < len2+1; i++) Result[i]=0;  for(inti =0; i < len1; i++){         for(intj = len2-1; J >=0; j--){            if(S1[i] = =S2[j]) {cout<<i<<" "<<j<<Endl; Result[j+1] = result[j]+1; }Else{result[j+1] =0; }            if(result[j+1] >longest) {Longest= result[j+1]; Index= j+1; }        }    }    returnS2.substr (index-longest,longest);}

(2) Public oldest firstborn sequence (discontinuous)

Non-contiguous state transitions are also easy to get.

D[I,J] = d[i-1,j-1]+1 (a[i] = = B[j])

D[I,J] = max (d[i-1,j],d[i,j-1]) (a[i]! = B[j])

Similarly, when dimensionality is reduced, j is still in reverse order.

intLcs_not_continue (stringS1,stringS2) {    intLen1 =s1.size (); intLen2 =s2.size (); Vector<int> Result (len2+1);  for(inti =0; I < len2+1; i++) Result[i]=0;  for(inti =0; i < len1; i++){         for(intj = len2-1; J >=0; j--){            if(S1[i] = =S2[j]) {Result[j+1] = result[j]+1; }Else{result[j+1] = max (result[j],result[j+1]); }        }    }    returnresult[len2];}

Leetcode:edit Distance

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.