对编辑距离感到困惑

时间:2016-11-05 06:03:00

标签: c algorithm edit-distance

使用编辑距离我必须找到两个字符串之间有多少编辑,我已经在下面的代码中完成了,但我坚持的部分是打印2d数组,输出假设如下所示: enter image description here

int editdistance(char *s, int ls, char *t, int lt)
{
    int a, b, c;
    if (!ls) return lt;

    if (!lt) return ls;

    if (s[ls] == t[ls])
            return editdistance(s, ls - 1, t, lt - 1);
    a = editdistance(s, ls - 1, t, lt - 1);
    b = editdistance(s, ls,     t, lt - 1);
    c = editdistance(s, ls - 1, t, lt    );

    if (a > b) a = b;
    if (a > c) a = c;

    return a + 1;
}

int main()
{
char s1[100];
char s2[100];
printf("first: \n");
scanf("%s",s1);
printf("second: \n");
scanf("%s",s2);

printf("edit distance: %d\n", editdistance(s1, strlen(s1), s2, strlen(s2)));

    return 0;
}

1 个答案:

答案 0 :(得分:0)

如果我是对的,你正在寻找Levenshtein distance。这是Dynamic programming中的算法。