参考代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s1 = scanner.nextLine(); String s2 = scanner.nextLine(); int n1 = s1.length(); int n2 = s2.length(); if(n1 == 0 || n2 == 0) { System.out.print(Math.max(n1, n2)); return; } int [][]dp = new int[n1 + 1][n2 + 1]; for(int i = 1; i <= n1; i++) dp[i][0] = i; for(int i = 1; i <= n2; i++) dp[0][i] = i; for(int i = 1; i <= n1; i++) for(int j = 1; j <= n2; j++) { int op1 = dp[i - 1][j] + 1; int op2 = dp[i][j - 1] + 1; int op3 = dp[i - 1][j - 1] + ((s1.charAt(i - 1) != s2.charAt(j - 1)) ? 1 : 0); dp[i][j] = Math.min(op1, Math.min(op2, op3)); } System.out.println(dp[n1][n2]); } }
0.0分
5 人评分
C语言程序设计教程(第三版)课后习题6.3 (C语言代码)浏览:543 |
字符串问题 (C语言代码)浏览:1634 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:583 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:387 |
C语言程序设计教程(第三版)课后习题6.6 (C语言代码)浏览:626 |
星期判断机 (C语言代码)浏览:892 |
川哥的吩咐 (C语言代码)浏览:663 |
C二级辅导-等差数列 (C语言代码)浏览:891 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:487 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:539 |