zoukankan      html  css  js  c++  java
  • LeetCode

    Edit Distance

    2014.2.25 23:07

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

    You have the following 3 operations permitted on a word:

    a) Insert a character
    b) Delete a character
    c) Replace a character

    Solution1:

      This is a typical problem of dynamic programming. Let f[i][j] be the edit distance of w1[1:i] and w2[1:j]:

        1. f[0][0] = 0, all empty

        2. f[i][0] = i, delete i letters

        3. f[0][j] = j, insert j letters

        4. f[i][j] could be f[i][j - 1] + 1, insert a letter.

        5. f[i][j] could be f[i - 1][j] + 1, delete a letter.

        6. If w1[i] == w2[j], f[i][j] could be f[i - 1][j - 1].

        7. If w1[i] != w2[j], f[i][j] could be f[i - 1][j - 1] + 1, replace a letter.

      f[i][j] will be the minimum of them all. The result is f[len1][len2].

      The solution uses O(n^2) time and space.

    Accepted code:

     1 // 2CE, 2WA, 1AC, standard DP problem.
     2 #include <algorithm>
     3 #include <string>
     4 using namespace std;
     5 
     6 class Solution {
     7 public:
     8     int minDistance(string word1, string word2) {
     9         int n1, n2;
    10         
    11         if (n1 < n2) {
    12             return minDistance(word2, word1);
    13         }
    14         
    15         n1 = (int)word1.length();
    16         n2 = (int)word2.length();
    17         if (n1 == 0) {
    18             return n2;
    19         } else if (n2 == 0) {
    20             return n1;
    21         }
    22         
    23         int **dp;
    24 
    25         dp = new int*[n1 + 1];
    26         dp[0] = new int[(n1 + 1) * (n2 + 1)];
    27         
    28         int i, j;
    29         for (i = 1; i <= n1; ++i) {
    30             dp[i] = dp[0] + (i * (n2 + 1));
    31         }
    32         
    33         dp[0][0] = 0;
    34         for (i = 1; i <= n1; ++i) {
    35             dp[i][0] = i;
    36         }
    37         for (j = 1; j <= n2; ++j) {
    38             dp[0][j] = j;
    39         }
    40         
    41         for (i = 1; i <= n1; ++i) {
    42             for (j = 1; j <= n2; ++j) {
    43                 dp[i][j] = min(dp[i - 1][j] , dp[i][j - 1]) + 1;
    44                 if (word1[i - 1] == word2[j - 1]) {
    45                     dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]);
    46                 } else {
    47                     dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 1);
    48                 }
    49             }
    50         }
    51         int result = dp[n1][n2];
    52         
    53         delete[] dp[0];
    54         delete[] dp;
    55         
    56         return result;
    57     }
    58 };

    Solution2:

      Obviously the space usage can be reduced to O(n) with proper adjustment.

      See the code below.

    Accepted code:

     1 // 1AC, space-optimized version using DP.
     2 #include <algorithm>
     3 #include <string>
     4 using namespace std;
     5 
     6 class Solution {
     7 public:
     8     int minDistance(string word1, string word2) {
     9         int n1, n2;
    10         
    11         if (n1 < n2) {
    12             return minDistance(word2, word1);
    13         }
    14         
    15         n1 = (int)word1.length();
    16         n2 = (int)word2.length();
    17         if (n1 == 0) {
    18             return n2;
    19         } else if (n2 == 0) {
    20             return n1;
    21         }
    22         
    23         int **dp;
    24 
    25         dp = new int*[2];
    26         dp[0] = new int[2 * (n2 + 1)];
    27         dp[1] = dp[0] + (n2 + 1);
    28         
    29         int i, j;
    30         
    31         for (j = 0; j <= n2; ++j) {
    32             dp[0][j] = j;
    33         }
    34         
    35         int flag = 1;
    36         for (i = 1; i <= n1; ++i) {
    37             dp[flag][0] = i;
    38             for (j = 1; j <= n2; ++j) {
    39                 dp[flag][j] = min(dp[!flag][j] , dp[flag][j - 1]) + 1;
    40                 if (word1[i - 1] == word2[j - 1]) {
    41                     dp[flag][j] = min(dp[flag][j], dp[!flag][j - 1]);
    42                 } else {
    43                     dp[flag][j] = min(dp[flag][j], dp[!flag][j - 1] + 1);
    44                 }
    45             }
    46             flag = !flag;
    47         }
    48         int result = dp[!flag][n2];
    49         
    50         delete[] dp[0];
    51         delete[] dp;
    52         
    53         return result;
    54     }
    55 };
  • 相关阅读:
    js中let和var定义变量的区别
    windows下开发PHP扩展dll(无需Cygwin)
    用VS开发PHP扩展
    破解电信光猫华为HG8120C关闭路由功能方法
    从程序员到项目经理(二十九):怎样写文档
    从程序员到项目经理(二十八):该死的结果导向(只看结果,不问过程到底行不行?)
    从程序员到项目经理(二十七):怎样给领导汇报工作
    从程序员到项目经理(二十六):项目管理不能浑水摸鱼
    从程序员到项目经理(二十五):对绩效考核的吐槽
    从程序员到项目经理(二十四):慎于问敏于行
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3568115.html
Copyright © 2011-2022 走看看