LeetCode72 编辑距离 - Go语言中文社区

LeetCode72 编辑距离


原题目

给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 。
你可以对一个单词进行如下三种操作:
插入一个字符
删除一个字符
替换一个字符
示例 1:
输入: word1 = “horse”, word2 = “ros”
输出: 3
解释:
horse -> rorse (将 ‘h’ 替换为 ‘r’)
rorse -> rose (删除 ‘r’)
rose -> ros (删除 ‘e’)
示例 2:
输入: word1 = “intention”, word2 = “execution”
输出: 5
解释:
intention -> inention (删除 ‘t’)
inention -> enention (将 ‘i’ 替换为 ‘e’)
enention -> exention (将 ‘n’ 替换为 ‘x’)
exention -> exection (将 ‘n’ 替换为 ‘c’)
exection -> execution (插入 ‘u’)

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/edit-distance

题目分析

思路过程可以如下:
需要求解的是word1编辑到word2所需要的步骤最小值,是不是可以先把word1的一个子串编辑成word2的一个子串?然后随着子串的长度逐渐变大,是否可以推导出结果?

用W1Si表示word1的子串(sub(0, i)),W2Sj表示word2的子串(sub(0, j));
二维数组dp[i][j] 代表把 W1Si编辑成 W2Sj所需要的最少步数;

假如现在word1为horse,word2为ros;
把horse转化为ros,可以转换思路,可在已知以下三种情况之下,再做一个额外的操作,实现把horse编辑为ros:
1、当前已经有hors编辑为ros的步骤,那么可以在原word1(horse)基础之上,删除最后的e,也可以得到ros;
2、当前已经有horse编辑成ro的步骤,那么可以在原word1(horse)基础之上,插入一个s,也可以得到ros;
3、当前已经有了hors编辑成ro的步骤,那么可以在原word1(horse)基础之上,把最后的一个e替换成s,也可以得到ros;
特殊情况下:
hors->ros,其实就等于hor->ro;

#状态转移方程
那么就可以认为上面三种情况下最小值,就是最终结果:
dp(horse->ros) = min{dp(hors->ros), dp(horse->ro), dp(hors->ro)} + 1;
即:dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1;
特殊情况下:
如果word1[i]==word2[j],那么dp[i][j]=dp[i-1][j-1];

#边界
现在来看边界:
边界是i=0或者j=0;
如果i=0,表示从horse的一个空子串(“”)编辑成ros的所有子串(""、“r”、“ro”、“ros”)所需要的步数,每一个都执行插入就可以了,结果为dp[0][j] = j;
如果j=0,表示从horse的所有子串(“”、“h”、“ho”、“hor”、“hors”、“horse”)编辑成ros的一个空子串(“”)所需要的步数,每一步都执行删除就可以了,结果为dp[i][0]=i;
例子
在这里插入图片描述
参考
作者:pentium
链接:https://leetcode-cn.com/problems/edit-distance/solution/gen-ju-qi-ta-jing-cai-de-jie-ti-zong-jie-chu-lai-d/
方法一:
递归超时
方法二:
记忆化搜索
方法三:
动态规划

完整代码

方法一:

int dis(char *word1,char *word2,int n,int m)
{
    if(n==0)
        return m;
    if(m==0)
        return n;
    int a=dis(word1,word2,n-1,m)+1;
    int b=dis(word1,word2,n,m-1)+1;
    int c=dis(word1,word2,n-1,m-1);
    if(word1[n-1]!=word2[m-1])
        c++;
        return fmin(a,fmin(b,c));
}

int minDistance(char * word1, char * word2){
    return dis(word1,word2,strlen(word1),strlen(word2));
}

方法二:

int memo[1000][1000];
int dis(char *word1,char *word2,int n,int m)
{
    if(n==0)
        return m;
    if(m==0)
        return n;
    if(memo[n-1][m-1])
        return memo[n-1][m-1];
    int a=dis(word1,word2,n-1,m)+1;
    int b=dis(word1,word2,n,m-1)+1;
    int c=dis(word1,word2,n-1,m-1);
    if(word1[n-1]!=word2[m-1])
        c++;
    memo[n-1][m-1]=fmin(a,fmin(b,c));
    return memo[n-1][m-1];
}

int minDistance(char * word1, char * word2){
    memset(memo,0,sizeof(memo));
    return dis(word1,word2,strlen(word1),strlen(word2));
}

方法三:

int minDistance(char * word1, char * word2){
    int len1=strlen(word1)+1;
    int len2=strlen(word2)+1;
    int **dp=(int **)malloc(sizeof(int *)*len1);
    for(int i=0;i<len1;i++)
    {
        dp[i]=(int *)malloc(sizeof(int)*len2);
    }
    for(int i=0;i<len1;i++)
        for(int j=0;j<len2;j++)
        {
            if(i==0&&j==0)
                dp[i][j]=0;
            else if(i==0)
                dp[i][j]=dp[i][j-1]+1;
            else if(j==0)
            dp[i][j]=dp[i-1][j]+1;
            else if(word1[i-1]==word2[j-1])
                dp[i][j]=dp[i-1][j-1];
            else
                dp[i][j]=fmin((dp[i-1][j-1]),fmin(dp[i-1][j],dp[i][j-1]))+1;
            
        }
    return dp[len1-1][len2-1];
}
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_44529350/article/details/100107210
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢