利用动态规划求字符串编辑距离,python,Java实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_24003469/article/details/88972822

最近正努力研究一些动态规划算法(应付实习中),意外间,在python中文社区看到一个求字符串编辑距离。不多说,上代码。本文摘自python中文社区:https://m.nowcoder.com/cts/cts-user-schedule?ctsTestUid=B0139D7EF1676B2B
感谢jclian提供讲解

def edit_distance(s1, s2):
    len_s1 = len(s1)
    len_s2 = len(s2)

    dp = [[0 for _ in range(len_s2+1)] for _ in range(len_s1 + 1)]
    for i in range(len_s1 + 1):
        for j in range(len_s2 + 1):
            if i == 0:
                dp[i][j] = j
            elif j == 0:
                dp[i][j] = i
            elif s1[i - 1] == s2[j - 1]:
                dp[i][j] = dp[i - 1][j - 1]
            else:
                dp[i][j] = 1 + min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1])

    return dp[len_s1][len_s2]

print(edit_distance("say","saturday"))

编辑距离问题
什么是两个字符串的编辑距离(edit distance)?给定字符串s1和s2,以及在s1上的如下操作:

插入(Insert)一个字符
移除(Remove)一个字符
替换(Replace)一个字符
试问最小需要多少次这样的操作才能使得s1转换为s2?

比如,单词“cat”和“hat”,这样的操作最少需要一次,只需要把“cat”中的“c”替换为“h”即可。单词“recall”和“call”,这样的操作最少需要两次,只需要把“recall”中的“r”和“e”去掉即可。单词“Sunday”和“Saturday”,这样的操作最少需要3次,在“Sunday”的“S”和“u”中插入“a”和“t”,再把“n”替换成“r”即可。

那么,是否存在一种高效的算法,能够快速、准确地计算出两个字符串的编辑距离呢?

动态规划算法
我们使用动态规划算法(Dynamic Programming)来计算出两个字符串的编辑距离。
我们从两个字符串s1和s2的最末端向前遍历来考虑。假设s1的长度为m,s2的长度为n,算法如下:

如果两个字符串的最后一个字符一样,那么,我们就可以递归地计算长度为m-1和n-1的两个字符串的情形;
如果两个字符串的最后一个字符不一样,那么,进入以下三种情形:
插入: 递归地计算长度为m和n-1的两个字符串的情形,这是因为在s1中的末端插入了一个s2的最后一个字符,这样s1和s2的末端字符一样,就是1中情形;
删除: 递归地计算长度为m-1和n的两个字符串的情形,这是在s1中的末端删除了一个字符;
替换: 递归地计算长度为m-1和n-1的两个字符串的情形,这是因为把s1中末端字符替换成了s2的最后一个字符,这样s1和s2的末端字符一样,就是1中情形;
这样,我们就有了子结构问题。对于动态规划算法,我们还需要一个初始化的过程,然后中间维护一张二维表即可。初始化的过程如下: 如果m为0,则至少需要操作n次,即在s1中逐个添加s2的字符,一共是n次;如果n为0,则至少需要操作m次,即把s1的字符逐个删除即可,一共是m次。

Java实现

package DP_example;


// 计算两个字符串的编辑距离(Edit Distance)
public class Edit_Distance {

   // 主函数
   public static void main(String[] args) {
       String str1 = "cat";//"Sunday";
       String str2 = "hat";//"Saturday";
       int edit_dist = edit_distance(str1, str2);
       System.out.println(String.format("The edit distance of '%s' and '%s' is %d.",
               str1, str2, edit_dist));
   }

   /*
   函数edit_distanc: 计算两个字符串的编辑距离(Edit Distance)
   传入参数:  两个字符串str1和str2
   返回: 编辑距离
    */
   public static int edit_distance(String str1, String str2){

       // 字符串的长度
       int m = str1.length();
       int n = str2.length();

       // 初始化表格,用于维护子问题的解
       int[][] dp = new int[m+1][n+1];
       for(int i=0; i <= m; i++)
           for(int j=0; j <= n; j++)
               dp[i][j] = 0;

       // using DP in bottom-up manner
       for(int i=0; i <= m; i++){
           for(int j=0; j <= n; j++) {
               /* If first string is empty, only option is to
                * isnert all characters of second string, thus the
                * min opration is j
                */
               if(i == 0) { dp[i][j] = j;}

               /* If second string is empty, only option is to
                * remove all characters of second string, thus the
                * min opration is i
                */
               else if(j == 0){dp[i][j] = i;}

               /* If last characters are same, ignore last character
                * and recursive for remaining string
                */
               else if(str1.charAt(i-1) == str2.charAt(j-1)){
                   dp[i][j] = dp[i-1][j-1];
               }

               /*If last character are different, consider all
                *possibilities and find minimum of inserting, removing, replacing
                */
               else{
                   /*
                    * dp[i][j-1]: Insert
                    * dp[i-1][j]: Remove
                    * dp[i-1][j-1]: Replace
                    */
                   dp[i][j] = 1 + min(min(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]);
               }
           }
       }

       return dp[m][n];
   }

   public static int min(int i, int j){
       return (i <= j) ? i : j;
   }

}

猜你喜欢

转载自blog.csdn.net/qq_24003469/article/details/88972822