求两个字符串的编辑距离

https://www.jianshu.com/p/a617d20162cf

代码:

float ldistance(const std::string source, const std::string target)
{
    int distance = 0;
    float similarity = 0.0;
    int srcLen = source.length();
    int tgtLen = target.length();

    //step 1
    if (0 == tgtLen || 0 == srcLen)
        return 0;
    //Construct a matrix
    typedef std::vector<std::vector<int> > Tmatrix;
    Tmatrix matrix(srcLen + 1);
    for (int i = 0; i <= srcLen; ++i)
        matrix[i].resize(tgtLen + 1);

    //step 2 Initialize
    for (int i = 1; i <= srcLen; ++i)
        matrix[i][0] = i;
    for (int i = 1; i <= tgtLen; ++i)
        matrix[0][i] = i;

    //step 3
    for (int i = 1; i <= srcLen; ++i) {
        const char si = source[i - 1];
        //step 4
        for (int j = 1; j <= tgtLen; ++j) {
            const char dj = target[j - 1];
            //step 5
            int cost;
            if (si == dj) {
                cost = 0;
            } else {
                cost = 1;
            }
            //step 6
            const int above = matrix[i - 1][j] + 1;
            const int left = matrix[i][j - 1] + 1;
            const int diag = matrix[i - 1][j - 1] + cost;

            int min = left > diag ? diag : left;
            min = min > above ? above : min;

            matrix[i][j] = min;

        }
    }//step7

    distance = matrix[srcLen][tgtLen];
    similarity = 1 - (float) distance / std::max(srcLen, tgtLen);
    return similarity;
}

 

posted @ 2019-06-24 15:09  rainsoul  Views(500)  Comments(0Edit  收藏  举报