我试图了解 Wagner–Fischer 算法来查找字符串之间的距离。我正在以下链接中查看它的伪代码:http ://en.wikipedia.org/wiki/Wagner%E2%80%93Fischer_algorithm
int EditDistance(char s[1..m], char t[1..n])
// For all i and j, d[i,j] will hold the Levenshtein distance between
// the first i characters of s and the first j characters of t.
// Note that d has (m+1) x(n+1) values.
let d be a 2-d array of int with dimensions [0..m, 0..n]
for i in [0..m]
d[i, 0] ← i // the distance of any first string to an empty second string
for j in [0..n]
d[0, j] ← j // the distance of any second string to an empty first string
for j in [1..n]
for i in [1..m]
if s[i] = t[j] then
d[i, j] ← d[i-1, j-1] // no operation required
else
d[i, j] ← minimum of
(
d[i-1, j] + 1, // a deletion
d[i, j-1] + 1, // an insertion
d[i-1, j-1] + 1 // a substitution
)
return d[m,n]
实际上我明白了算法的要点并直观地理解它,因为它对我来说并不明显为什么d[i-1, j] + 1, d[i, j-1] + 1 和 d[i-1, j-1 ] + 1被认为是删除、插入和替换。如果有人以更详细的方式解释实现的细微差别,那就太好了。