import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()){
String str1 = sc.nextLine();
String str2 = sc.nextLine();
System.out.println(dp(str1,str2));
}
}
public static int dp(String str1 , String str2 ){
char [] wd1 = str1.toCharArray();
char [] wd2 = str2.toCharArray();
int len1 = wd1.length;
int len2 = wd2.length;
int [][] dp = new int[len1+1][len2+1];
for (int i = 0; i <= len1 ; i++) {
dp[i][0]=i;
}
for (int i = 0; i <= len2 ; i++) {
dp[0][i]=i;
}
for (int i = 1; i <=len1 ; i++) {
for (int j = 1; j <=len2 ; j++) {
dp[i][j] = Math.min(dp[i-1][j],dp[i][j-1])+1;
if (wd1[i-1]==wd2[j-1]){
dp[i][j]= Math.min(dp[i][j],dp[i-1][j-1]);
}else {
dp[i][j]= Math.min(dp[i][j],dp[i-1][j-1]+1);
}
}
}
return dp[len1][len2];
}
}
每日一题 5-14
最新推荐文章于 2022-12-03 10:56:33 发布