Topic:
Source: The beauty of programming
To change the two string into the same basic operation is defined as follows:
1. Modify a character (e.g. turn A into B)
2. Add a character (e.g. Abed into Abedd)
3. Delete a character (such as Jeddon into Jedon)
For Jeddon to Jedon you only need to delete one or more D to turn two strings into the same. To define the number of times required for this operation as the distance L for two strings, the similarity is defined as 1/(l+1) that is the reciprocal of the distance plus one. So the similarity between Jeddon and Jedon is 1/1+1=1/2=0.5 and the similarity of two strings is 0.5.
Given any two strings, do you write out whether or not to calculate their acquaintance.
Analysis:
This is a multi-stage decision-making problem, and each stage of decision-making is related to the optimal decision of the front face problem, which can be done by dynamic programming.
For strings str1 and STR2, the lengths are M,n, and the distance between STR1 's [1-i] character and Str2 's [1-j] character is set to D[i,j], respectively.
If STR1[I]=STR2[J], then d[i,j]=d[i-1,j-1];
If Str1[i] and Str2[j] replace one character, Str1[0-i] and str2[0-j] are the same, such as "student" and "Studens", then d[i,j]=d[i-1,j-1]+1;
If Str1[i] adds one character, Str1[0-i] and str2[0-j] are the same, such as "student" and "students", then d[i,j]=d[i,j-1]+1;
If Str1[i] Deletes a character, str1[0-i] and str2[0-j] are the same, such as "students" and "student", then d[i,j]=d[i-1,j]+1;
Thus the relationship can be obtained:
D[i,0]=i
D[0,j]=j;
D[I,J]=MIN{D[I-1,J-1]+1,D[I,J-1]+1,D[I-1,J]+1}
Algorithm time complexity is O (m*n), Space complexity is O (m*n)
Code
int Similaritydegreeofstrs (char* str1,char* str2) {int Len1=strlen (STR1); int Len2=strlen (STR2); int **d=new int*[len1+1 ]; for (int i=0;i<=len1;i++) {d[i]=new int[len2+1] (),} for (int. i=0;i<=len1;i++) {d[i][0]=i;} for (int j=0;j<=le n2;j++) {d[0][j]=j;} for (int i=1;i<=len1;i++) {for (int j=1;j<=len2;j++) {if (str1[i-1]==str2[j-1]) {D[i][j]=d [I-1] [J-1]; }else {int repl=d[i-1][j-1]+1; int add=d[i][j-1]+1; int del=d[i-1][j]+1; D[i][j]=min (MIN (Repl,add), Del);}} } int similarity=d[len1][len2]/(d[len1][len2]+1); Free for (int i=0;i<=len1;i++) {delete[] d[i];} delete[] D; return similarity;}
Algorithm 8 String similarity