This problem, I met in Alibaba interview, Google's pen questions ...
Set A and B are 2 strings. To convert string A to string B with a minimum of character manipulation. The character operations described here include:
(1) Delete a character;
(2) inserting a character;
(3) Change one character to another character.
The minimum character operand used to transform string A into string B is called the editing distance of the string A through B, which is recorded as D (A, A, a). Try to design an effective algorithm for the given 2 strings A and B, to calculate their editing distance d (A, a, a, a,).
Method 1: Recursive method of programming beauty
#include <iostream> #include <string> using namespace std;
int Min (int a,int b,int c) {int t = a<b?a:b;
Return t<c?t:c; } int caldistance (const string &stra,int pabegin,int paend, const string &strb,int pbbegin,int pbend) {if (P
Abegin>paend) {if (pbbegin>pbend) return 0;
else return pbend-pbbegin+1;
} if (Pbbegin>pbend) {if (pabegin>paend) return 0;
else return paend-pabegin+1;
} if (stra[pabegin] = = Strb[pbbegin]) {return caldistance (stra,pabegin+1,paend,strb,pbbegin+1,pbend); } else{int disa = caldistance (stra,pabegin+1,paend,strb,pbbegin+1,pbend) + 1;//Replace int disb = Caldistance (strA,pAbegi N,paend,strb,pbbegin+1,pbend) + 1;//a Insert int disc = Caldistance (stra,pabegin+1,paend,strb,pbbegin,pbend) + 1;//b Insert Retu
RN Min (DISA,DISB,DISC);
}} int main () {string stra = "LSJD";
string strB = "ls";
Cout<<caldistance (Stra,0,stra.size () -1,strb,0,strb.size ()-1) <<endl;
System ("pause");
return 0; }
There is a drawback to this approach: there are recurring nodes on the recursive tree. We can pass
the method of the memoTo record some values, thereby reducing the number of repetitions.
Method 2: Dynamic Programming Solution
A summary of the experience: like this from the back by the front push (top-down) can be converted to a dynamic programming algorithm (bottom-up). Included on this blog: Fibonacci sequence, complete knapsack problem, fish fishing problem.
We can draw a dynamic programming formula: (The following quote: http://qinxuye.me/article/get-edit-distance-by-dynamic-programming/;http:// blog.chinaunix.net/uid-20761674-id-75042.html)
(1) If i = = 0 and J = = 0,edit (i, J) =0
(2) If i = = 0 and J > 0,edit (i, J) =j
(3) If I > 0 and j = = 0,edit (i, J) =i (2, 3 points have been stated before)
(4) If 0 < i≤1 and 0 < j≤1, edit (i, j) = = min{Edit (i-1, J) + 1, edit (i, j-1) + 1, edit (I-1,j-1) + f (i, j)}, where the string 1 The I character is not equal to the first J character of the String 2, F (i, j) = 1; otherwise, f (i, j) = 0.
(5) If I > 1 and J > 1 o'clock, this time may appear operation (4), from the previous deduction, we can only exchange once, otherwise there is no meaning. At this time it is possible to add edit (i-2, j-2) +1 to the minimum value, and when to join. Suppose that the i-2 length string 1 substring and j-2 length string 2 substring have been derived from the optimal solution, this time if s1[i-1] = = S2[j] and s1[i] = = S2[j-1], then the comparison value is added to edit (i-2, j-2) + 1 (This 1 is the operation of the exchange once)
That is, the final recursive formula: first given the first row and first column, then each value d[i,j] This calculation: d[i][j] = min (d[i-1][j]+1,d[i][j-1]+1,d[i-1][j-1]+ (s1[i] = = s2[j]?0:1));
Code:
#include <stdio.h>
#include <string.h>
char s1[1000],s2[1000];
int min (int a,int b,int c) {
int t = a < b? a:b;
Return T < c? t:c;
}
void editdistance (int len1,int len2) {
int d[len1+1][len2+1]; In fact, to dynamically allocate
int i,j;
for (i = 0;i <= len1;i++)
d[i][0] = i; I start with the 1th, representing the 1th letter, so the No. 0 one does not count for
(j = 0;j <= len2;j++)
d[0][j] = j;
for (i = 1;i <= len1;i++) for
(j = 1;j <= len2;j++) {
int. cost = s1[i] = = S2[j]? 0:1;
int deletion = D[i-1][j] + 1;
int insertion = d[i][j-1] + 1;
int substitution = d[i-1][j-1] + cost;
D[i][j] = min (deletion,insertion,substitution);
}
printf ("%d\n", D[len1][len2]);
}