Migrated to: http://www.wypblog.com/archives/58
The idea is the beauty of programming. I just made a little optimization...
Many programs use strings in large quantities. For different strings, we hope to be able to determine their similar programs. We define a set of operation methods to make the two strings different from each other the same. The specific operation method is:
1. modify a character (for example, replace "A" with "B ");
2. Add a character (for example, change "abdd" to "aebdd ");
3. delete a character (for example, change "traveling" to "traveling ");
For example, for the "abcdefg" and "abcdef" strings, we think we can increase/decrease a "G" to achieve the goal. The preceding two solutions only need to be used once. The number of times required for this operation is defined as the distance between two strings, and the similarity is equal to the reciprocal of "distance + 1. That is to say, the distance between "abcdefg" and "abcdef" is 1, and the similarity is 1/2 = 0.5.
Given any two strings, can you write an algorithm to calculate their similarity?
Analysis and Solution of the original text
It is not hard to see that the distance between two strings must not exceed the sum of their lengths (we can convert both strings into empty strings through the delete operation ). Although this conclusion does not help the result, we can at least know that the distance between any two strings is limited. We still need to focus on how we can turn this problem into a smaller subproblem. If there are two strings a = xabcdae and B = xfdfa, their first character is the same, as long as a [2 ,..., 7] = abcdae and B [2 ,..., 5] = FDFA distance. However, if the first character of the two strings is different, you can perform the following operations (Lena and lenb are the length of string a and string B respectively ).
1. Delete the first character of string a, and then calculate the distance between a [2,..., Lena] and B [1,..., lenb.
2. Delete the first character of string B, and then calculate the distance between a [1,..., Lena] and B [2,..., lenb.
3. modify the first character of string a to the first character of string B, and then calculate a [2 ,..., lena] and B [2 ,..., lenb.
4. modify the first character of string B to the first character of string a, and then calculate a [2 ,..., lena] and B [2 ,..., lenb.
5. add the first character of string B before the first character of string a, and then calculate a [1 ,..., lena] and B [2 ,..., lenb.
6. add the first character of string a before the first character of string B, and then calculate a [2 ,..., lena] and B [1 ,..., lenb.
In this question, we do not care what the strings are after the two strings become equal. Therefore, you can merge the above six operations:
1. After one step, convert a [2,..., Lena] and B [1,..., lenb] into a phase string.
2. After one step, convert a [2,..., Lena] and B [2,..., lenb] into a phase string.
3. After one step, convert a [1,..., Lena] and B [2,..., lenb] into a phase string.
In this way, a recursive program can be completed quickly.
However, when the source program is called recursively, many processes are computed repeatedly, resulting in low time efficiency. We can use an array to record the calculated results. If the next recursion is required, first check whether the values in the array are not 0. If not, it indicates that the values have not been calculated, we need to calculate it. If yes, it indicates that the program has computed this result and does not need to be recursive again. Therefore, we can directly read the values in the array and return the result.
#include <iostream>using namespace std;/****Create By: w397090770* Data : 2012.10.14*Email: wyphao.2007@163.com**/ int min(int a, int b){return a > b ? b : a;} int EditDistence(char *str1, int aStart, int aEnd, char *str2, int bStart, int bEnd, int temp[][100]){if(aStart > aEnd){if(bStart > bEnd){return 0;}else{return bEnd - bStart + 1;}} if(bStart > bEnd){if(aStart > aEnd){return 0;}else{return aEnd - aStart + 1;}}if(str1[aStart] == str2[bStart]){int a = 0;if(temp[aStart + 1][bStart + 1] != 0){a = temp[aStart + 1][bStart + 1];}else{a = EditDistence(str1, aStart + 1, aEnd, str2, bStart + 1, bEnd, temp);temp[aStart + 1][bStart + 1] = a;}return a;}else{int a, b, c;if(temp[aStart + 1][bStart + 1] != 0){a = temp[aStart + 1][bStart + 1];}else{a = EditDistence(str1, aStart + 1, aEnd, str2, bStart + 1, bEnd, temp);temp[aStart + 1][bStart + 1] = a;}if(temp[aStart + 1][bStart] != 0){b = temp[aStart + 1][bStart];}else{b = EditDistence(str1, aStart + 1, aEnd, str2, bStart, bEnd, temp);temp[aStart + 1][bStart] = b;}if(temp[aStart][bStart + 1] != 0){c = temp[aStart][bStart + 1];}else{c = EditDistence(str1, aStart, aEnd, str2, bStart + 1, bEnd, temp);temp[aStart][bStart + 1] = c;}return min(min(a, b), c) + 1;}}int main(){char *str1 = "efsdfdabcdefgaabcdefgaabcdefgaabcdefgasfabcdefgefsdfdabcdefgaabcdefgaabcdefgaabcdefgasfabcdefg";char *str2 = "efsdfdabcdefgaabcdefgaaefsdfdabcdefgaabcdefgaabcdefgaabcdefgasfabcdabcdefggaabcdefgasfabcdefg";int temp[100][100];memset(temp, 0, sizeof(temp));cout << EditDistence(str1, 0, strlen(str1), str2, 0, strlen(str2), temp) << endl; return 0;}