📜  编辑距离和LCS(最长公共子序列)

📅  最后修改于: 2021-04-28 18:34:46             🧑  作者: Mango

在允许3个操作的标准“编辑距离”中,插入,删除和替换。考虑一个编辑距离的变化,在这里我们只允许两个操作插入和删除,在这个变化中找到编辑距离。

例子:

Input : str1 = "cat", st2 = "cut"
Output : 2
We are allowed to insert and delete. We delete 'a'
from "cat" and insert "u" to make it "cut".

Input : str1 = "acb", st2 = "ab"
Output : 1
We can convert "acb" to "ab" by removing 'c'.

一种解决方案是通过进行两次而不是三次的递归调用来简单地修改“编辑距离解决方案”。一个有趣的解决方案基于LCS。
1)找到两个字符串的LCS。令LCS的长度为x
2)假设第一个字符串的长度为m ,第二个字符串的长度为n 。我们的结果是(m – x)+(n – x)。我们基本上需要执行(m-x)删除操作和(n-x)插入操作。

C++
// CPP program to find Edit Distance (when only two
// operations are allowed, insert and delete) using LCS.
#include
using namespace std;
 
int editDistanceWith2Ops(string &X, string &Y)
{
    // Find LCS
    int m = X.length(), n = Y.length();
    int L[m+1][n+1];
    for (int i=0; i<=m; i++)
    {
        for (int j=0; j<=n; j++)
        {
            if (i == 0 || j == 0)
                L[i][j] = 0;
            else if (X[i-1] == Y[j-1])
                L[i][j] = L[i-1][j-1] + 1;
            else
                L[i][j] = max(L[i-1][j], L[i][j-1]);
        }
    }   
    int lcs  = L[m][n];
 
    // Edit distance is delete operations +
    // insert operations.
    return (m - lcs) + (n - lcs);
}
 
/* Driver program to test above function */
int main()
{
    string X = "abc", Y = "acd";
    cout << editDistanceWith2Ops(X, Y);
    return 0;
}


Java
//Java program to find Edit Distance (when only two
// operations are allowed, insert and delete) using LCS.
 
class GFG {
 
    static int editDistanceWith2Ops(String X, String Y) {
        // Find LCS
        int m = X.length(), n = Y.length();
        int L[][] = new int[m + 1][n + 1];
        for (int i = 0; i <= m; i++) {
            for (int j = 0; j <= n; j++) {
                if (i == 0 || j == 0) {
                    L[i][j] = 0;
                } else if (X.charAt(i - 1) == Y.charAt(j - 1)) {
                    L[i][j] = L[i - 1][j - 1] + 1;
                } else {
                    L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]);
                }
            }
        }
        int lcs = L[m][n];
 
        // Edit distance is delete operations +
        // insert operations.
        return (m - lcs) + (n - lcs);
    }
 
    /* Driver program to test above function */
    public static void main(String[] args) {
        String X = "abc", Y = "acd";
        System.out.println(editDistanceWith2Ops(X, Y));
 
    }
}
/* This Java code is contributed by 29AjayKumar*/


Python 3
# Python 3 program to find Edit Distance
# (when only two operations are allowed,
# insert and delete) using LCS.
 
def editDistanceWith2Ops(X, Y):
 
    # Find LCS
    m = len(X)
    n = len(Y)
    L = [[0 for x in range(n + 1)]
            for y in range(m + 1)]
    for i in range(m + 1):
        for j in range(n + 1):
            if (i == 0 or j == 0):
                L[i][j] = 0
            elif (X[i - 1] == Y[j - 1]):
                L[i][j] = L[i - 1][j - 1] + 1
            else:
                L[i][j] = max(L[i - 1][j],
                              L[i][j - 1])
 
    lcs = L[m][n]
 
    # Edit distance is delete operations +
    # insert operations.
    return (m - lcs) + (n - lcs)
 
# Driver Code
if __name__ == "__main__":
     
    X = "abc"
    Y = "acd"
    print(editDistanceWith2Ops(X, Y))
 
# This code is contributed by ita_c


C#
// C# program to find Edit Distance
// (when only two operations are
// allowed, insert and delete) using LCS.
using System;
 
class GFG
{
 
static int editDistanceWith2Ops(String X,
                                String Y)
{
    // Find LCS
    int m = X.Length, n = Y.Length;
    int [ , ]L = new int[m + 1 , n + 1];
    for (int i = 0; i <= m; i++)
    {
        for (int j = 0; j <= n; j++)
        {
            if (i == 0 || j == 0)
            {
                L[i , j] = 0;
            }
            else if (X[i - 1] == Y[j - 1])
            {
                L[i , j] = L[i - 1 , j - 1] + 1;
            }
            else
            {
                L[i , j] = Math.Max(L[i - 1 , j],
                                    L[i , j - 1]);
            }
        }
    }
    int lcs = L[m , n];
 
    // Edit distance is delete operations +
    // insert operations.
    return (m - lcs) + (n - lcs);
}
 
// Driver Code
public static void Main()
{
    String X = "abc", Y = "acd";
    Console.Write(editDistanceWith2Ops(X, Y));
}
}
 
// This code is contributed
// by 29AjayKumar


PHP


输出:

2

时间复杂度: O(m * n)
辅助空间: O(m * n)