Title Link: http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1284
Edit Distance
Time limit:1000 MS Memory limit:65536 K
Total submit:354 (134 users) Total accepted:186 (127 users) Rating:special Judge:no
Description
Russian scientist Vladimir
Levenshtein presented the concept of editorial distance in 1965. The editing distance, also known as the Levenshtein distance, is the minimum number of edit operations required to turn from one two string to another. The three editing operations that are permitted include inserting a character, deleting one character, and substituting one character for another character.
So far, editing distance has been playing an important role in the field of similar sentence retrieval.
We might as well design a program to calculate the editing distance of two strings.
Input
The first line of the input data is a positive integer representing a total of several sets of data.
Each group of data has two lines, one string per line.
Each string length does not exceed 1000
The string contains only lowercase English letters
Output
For each set of data, output an integer that represents the editing distance of two strings.
Each answer takes up one line.
Sample Input
2
David
Vivian
Abc
Aabbcc
Sample Output
4
3
"Thought Analysis" dp[i][j] represents the number of times a 0-i character in the first string becomes 0-j in the second string.
Let's analyze it in several parts below.
1.i=0 when the dp[0][j]=j;
2.j=0 when the dp[i][0]=i;
3.i and J!=0.
if (Str1[i-1]==str2[j-1])
{
dp[i][j]=min (dp[i-1][j]+1,min (dp[i][j-1]+1,dp[i-1][j-1]));
}
else
{
dp[i][j]=min (dp[i-1][j]+1,min (dp[i][j-1]+1,dp[i-1][j-1]+1));
}
"AC Code"
#include <cstdio> #include <cstring> #include <map> #include <cmath> #include <algorithm>
using namespace Std;
int dp[1005][1005];
Char str1[1005],str2[1005];
int main () {int n;
scanf ("%d", &n);
while (n--) {int num;
scanf ("%s", str1);
scanf ("%s", str2);
int Len1=strlen (STR1), Len2=strlen (STR2);
for (int i=0;i<=len1;i++) {dp[i][0]=i;
} for (int i=0;i<=len2;i++) {dp[0][i]=i; } for (int i=1;i<=len1;i++) {for (int j=1;j<=len2;j++) {if (s
Tr1[i-1]==str2[j-1]) {dp[i][j]=min (Dp[i-1][j]+1,min (dp[i][j-1]+1,dp[i-1][j-1])); } else {dp[i][j]=min (Dp[i-1][j]+1,min (dp[i][j-1]+1,dp[i-
1][j-1]+1));
}}} printf ("%d\n", Dp[len1][len2]); } rEturn 0;
}