计算两个字符串编辑距离
2016-06-22 16:31
281 查看
public static int levenDistance(String s, String t) { if (s == null || t == null) { return 10; } s = s.toLowerCase(); t = t.toLowerCase(); /* if (Math.abs(s.length() - t.length()) >= 3) { return 3; }*/ int d[][]; // matrix int n; // length of s int m; // length of t int i; // iterates through s int j; // iterates through t char s_i; // ith character of s char t_j; // jth character of t int cost; // cost // Step 1 n = s.length(); m = t.length(); if (n == 0) { return m; } if (m == 0) { return n; } d = new int[n + 1][m + 1]; // Step 2 for (i = 0; i <= n; i++) { d[i][0] = i; } for (j = 0; j <= m; j++) { d[0][j] = j; } // Step 3 for (i = 1; i <= n; i++) { s_i = s.charAt(i - 1); // Step 4 for (j = 1; j <= m; j++) { t_j = t.charAt(j - 1); // Step 5 if (s_i == t_j) { cost = 0; } else { cost = 1; } // Step 6 // d[i][j] = Minimum(d[i - 1][j] + 1, d[i][j - 1] + 1, // d[i - 1][j - 1] + cost); // 求三个数的最小值(a<b?a:b)<c?(a<b?a:b):c d[i][j] = ((d[i - 1][j] + 1) < (d[i][j - 1] + 1) ? (d[i - 1][j] + 1) : (d[i][j - 1] + 1)) < (d[i - 1][j - 1] + cost) ? ((d[i - 1][j] + 1) < (d[i][j - 1] + 1) ? (d[i - 1][j] + 1) : (d[i][j - 1] + 1)) : (d[i - 1][j - 1] + cost); } } // Step 7 return d [m]; }
相关文章推荐
- svn服务配置和日常维护命令
- Mysql 导出导入
- 微信Android客户端架构演进之路-简单总结
- Picasso源码分析(六):BitmapHunter与请求结果的处理
- python读取特定的行
- 面试题
- 关于文件
- SVN远程密码修改本地无法登陆的问题修复
- Android 开发值得订阅的日报和周刊
- okhttp3+retrofit2进行HTTP请求以及设置请求超时示例
- Signalr信息推送
- 关于ES6的 模块功能 Module 中export import的用法和注意之处
- GUID
- Linux vi编辑器的基本命令
- Citrix Xenserver 6 license激活三步骤
- 2016.6.22
- 【java】Cookie实现简单浏览商品
- Longest Increasing Subsequence
- JavaScript基于原型链的继承
- easyUI 获取Json 嵌套数据