2 条题解

  • 1
    @ 2024-4-12 14:29:31
    import java.util.Scanner;
    
    public class Main {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
    
            String x=sc.next();
            String y=sc.next();
            int[][] dp=new int[x.length()+1][y.length()+1];
            //初始化
            for (int i = 0; i <= x.length(); i++) {
                dp[i][0]=i;
            }
            for (int j = 0; j <= y.length(); j++) {
                dp[0][j]=j;
            }
    
    
            for (int i = 0; i < x.length(); i++) {
                for (int j = 0; j < y.length(); j++) {
    
                    if (x.charAt(i)!=y.charAt(j)){
                        dp[i+1][j+1]=Math.min(  dp[i][j]+1 , Math.min( dp[i][j+1]+1,dp[i+1][j]+1 ) );
                    }else{
                        dp[i+1][j+1]=dp[i][j];
                    }
                }
            }
            System.out.println(dp[x.length()][y.length()]);
    
        }
    }
    

    信息

    ID
    124
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    153
    已通过
    38
    上传者