子串的不对称编辑距离

时间:2019-12-25 14:42:39

标签: c++ string levenshtein-distance

我计算了两个字符串之间的不等距编辑距离:唯一允许的操作是从第二个字符串s2中删除元素。删除的费用为1。

int get_edit_distance(
        const std::string& s1, const std::string& s2) {
    int n = s1.length();
    int m = s2.length();
    int UPPER_COST_BOUNDARY = 99999;

    int d[n+1][m+1];
    for (int i=0; i<=n; i++) { d[i][0] = UPPER_COST_BOUNDARY; }
    for (int j=0; j<=m; j++) { d[0][j] = j; }

    for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            if (s1[i-1] != s2[j-1]) {
                d[i][j] = d[i][j-1] + 1; // deletion
            } else {
                d[i][j] = d[i-1][j-1]; // rewrite same character
            }
        }
    }
}

现在,假设我有s1个“堆栈”。我希望能够获得删除该字符串的前/后元素所得到的子字符串的编辑距离。

我知道我可以通过仅获取d数组的相应列来获得“ S”,“ ST”,“ STA”和“ STAC”的编辑距离。

但是如何从{STACK“的d数组中获得” TACK“,” ACK“,” CK“和” K“的编辑距离?

如何将两者结合?即获取子字符串“ TAC”,“ AC”等的编辑距离?我真的应该直接为所有此类子字符串重新计算吗?还是有一种方法可以根据为d计算的s1 = "STACK"推论得出?

如果它是对称的,我只需要选择相应的行(或者我认为是这样)。但是它不像我的代码中那样对称,所以我将如何进行?

0 个答案:

没有答案
相关问题