动态规划求编辑距离。允许的操作有:delete, insert, replace
#include <iostream>
using namespace std;
const int MAXLEN = 100;
int min(int a, int b, int c){
int small = a < b ? a : b;
return small < c ? small : c;
}
int edit_distance(char* word1, char* word2, int len1, int len2){
int mat[MAXLEN][MAXLEN];
for (int i = 0; i <= len1; ++i)
mat[i][0] = i;
for (int j = 0; j <= len2; ++j)
mat[0][j] = j;
for (int i = 1; i <= len1; ++i){
for (int j = 1; j <= len2; ++j){
int dif = word1[i - 1] == word2[j - 1] ? 0 : 1;
mat[i][j] = min(mat[i - 1][j] + 1, mat[i][j - 1] + 1, mat[i - 1][j - 1] + dif);
}
}
return mat[len1][len2];
}
void main(int argc, char* argv[])
{
char* w1 = "kitten";
char* w2 = "sitting";
cout << edit_distance(w1, w2, 6, 7);
system("pause");
}