编辑距离(一)

题目描述

给定两个字符串 str1 和 str2 ,请你算出将 str1 转为 str2 的最少操作数。

你可以对字符串进行3种操作:

1.插入一个字符

2.删除一个字符

3.修改一个字符。

字符串长度满足 1 ≤ n ≤ 1000 ,1≤n≤1000  ,保证字符串中只出现小写英文字母。

输入描述

输入:“nowcoder”,“new”

说明:

"nowcoder"=>"newcoder"(将'o'替换为'e'),修改操作1次

"nowcoder"=>"new"(删除"coder"),删除操作5次

输出描述

返回值:6

Sample Input

intention

execution

Sample Output

5

题目链接:编辑距离(一)_牛客题霸_牛客网

        目标字符串(str2)作为二维数组的行,转换前的字符串(str1)作为二位数组的列,(i,j)位置表示str1截止到 i 之前的字符串转换为str2截止到 j 之前的字符串所需要最少的步数,只依赖于str[i-1]和str[j-1]。

动态规划: 

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String str1=sc.nextLine();
        String str2=sc.nextLine();
        System.out.println(editDistance(str1,str2));
    }
    public static int editDistance (String str1, String str2) {
        int n = str1.length() + 1;
        int m = str2.length() + 1;
        int[][] dp = new int[m][n];
        dp[0][0] = 0;
        int min;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 && j != 0) {
                    dp[i][j] = dp[i][j - 1] + 1;
                } else if (j == 0 && i != 0) {
                    dp[i][j] = dp[i - 1][j] + 1;
                } else if (j != 0 && i != 0) {
                    if (str1.charAt(j - 1) == str2.charAt(i - 1)) {
                        min = dp[i - 1][j - 1];
                    } else {
                        min = dp[i - 1][j - 1] + 1;
                    }
                    min = Math.min(min, dp[i - 1][j] + 1);
                    min = Math.min(min, dp[i][j - 1] + 1);
                    dp[i][j] = min;
                }
            }
        }
        return dp[m - 1][n - 1];
    }
}

空间压缩:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String str1=sc.nextLine();
        String str2=sc.nextLine();
        System.out.println(editDistance(str1,str2));
    }
    public static int editDistance (String str1, String str2) {
        int[] dp=new int[str2.length()+1];
        for(int i=1;i<dp.length;i++)
            dp[i]=dp[i-1]+1;
        for(int i=1;i<=str1.length();i++){
            int t1=dp[0];
            dp[0]=dp[0]+1;
            for(int j=1;j<=str2.length();j++){
                int t2=dp[j];
                if(str1.charAt(i-1)==str2.charAt(j-1)){
                    dp[j]=t1;
                }else{
                    dp[j]=t1+1;
                }
                dp[j]=Math.min(Math.min(dp[j],t2+1),dp[j-1]+1);
                t1=t2;
            }
        }
        return dp[str2.length()];
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ლ旺旺掀被

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值