1072: editing distance
Time limit (Common/Java): 1000 ms/10000 ms memory limit: 65536 Kbyte
Total submissions: 917 pass the test: 275
Description
Assume that the basic operations of a string are only: delete one character, insert one character, and modify one character to another.
We call any of the above three operations a step-by-step basic operation.
Next we define the editing distance between two strings: For two strings a and B, through the above basic operations, we can convert A to B or B to, the minimum number of operation steps required for converting string a to string B is the distance between string a and string B.
For example, if a = "ABC", B = "cbcd", the distance between A and B is 2.
Your task is to compile a quick program to calculate the editing distance between any two strings.
Input
The input contains multiple groups of test data. One row of test data in each group, which is string a and string B.
The length of a string is not greater than 1024 characters, and all characters are letters.
Output
Editing distance.
Sample Input
ABC cbcd
Sample output
2
Prompt
I believe that the dynamic planning algorithm can solve this problem, because I did this. Pai_^
Question Source
Zjgsu
# Include <stdio. h> # include <string. h> int DP [1029] [1029]; int main () {char str1 [1029], str2 [1029]; while (scanf ("% S % s", str1, str2)> 0) {int len1, len2; len1 = strlen (str1); len2 = strlen (str2); For (INT I = 0; I <= len2; I ++) // initialize DP [0] [I] = I; for (INT I = 0; I <= len1; I ++) DP [I] [0] = I; for (INT I = 1; I <= len1; I ++) for (Int J = 1; j <= len2; j ++) if (str1 [I-1] = str2 [J-1]) {DP [I] [J] = DP [I-1] [J-1];} else {DP [I] [J] = DP [I-1] [J] + 1; // remove character str1 [I] If (DP [I] [J-1] + 1 <DP [I] [J]) DP [I] [J] = DP [I] [J-1] + 1; // Add character str2 [J] To str1 if (DP [I-1] [J-1] + 1 <DP [I] [J]) DP [I] [J] = DP [I-1] [J-1] + 1; // converts str1 [I] To str2 [J]} printf ("% d \ n ", DP [len1] [len2]);}
Tzoj 1072: editing distance (Dynamic Planning)