字符串距离(java)

计算字符串的相似度

http://www.nowcoder.com/questionTerminal/f549ee08ddd84b8485a4fa9aefaf4a38

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            String s1 = in.nextLine();
            String s2 = in.nextLine();
            int[][] dp = new int[s1.length()+1][s2.length()+1];
            for (int i = 0; i <= s1.length(); i++){
                for (int j = 0; j <= s2.length(); j++){
                    if (i == 0){
                        dp[i][j] = j;
                    }else if (j == 0){
                        dp[i][j] = i;
                    }else if(s1.charAt(i-1) == s2.charAt(j-1)){
                        dp[i][j] = dp[i - 1][j - 1];
                    }else{
                        dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i - 1][j - 1]), dp[i][j - 1]) + 1;
                    }
                }
            }
            String res = "1/";
            res += dp[s1.length()][s2.length()] + 1;
            System.out.println(res);
        }
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
10-05 10:13
已编辑
HHHHaos:让这些老登来现在秋招一下,简历都过不去
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务