百度-20120926-字符串编辑距离
来源:互联网 发布:什么软件可以修复ie 编辑:程序博客网 时间:2024/06/06 12:53
题目:找出字符串的编辑距离,即统计一个字符串s1最少经过多少步操作变成字符串s2?(操作有三种:添加一个字符,删除一个字符,修改一个字符)。
备注:1)一直都推荐阅读最精彩的参考资料,关于Levenshtein distance也即edit distance,请参考wiki:http://en.wikipedia.org/wiki/Levenshtein_distance,wiki给出了非常详细的解答,相信读过之后会让你有收获。2)这题出现了很多大公司的笔试/面试题中,比如google,百度 and so on.
// 这是递归的解法,空间复杂度相当高,不实用// calculate the edit distance between source and target recursivelyint editDistanceRecursive(string source, string target){int len_source = source.length();int len_target = target.length();// test for degenerate cases of empty string of source or target stringif(len_source == 0)return len_target;if(len_target == 0)return len_source;int cost = 0;// check the last character of source and target is equal or notif(source[len_source-1] != target[len_target-1])cost = 1;return minimum(editDistanceRecursive(source.substr(0, len_source-1), target)+1, editDistanceRecursive(source, target.substr(0, len_target-1))+1, editDistanceRecursive(source.substr(0, len_source-1), target.substr(0, len_target-1))+cost);}
// 面试中应该倾向采用非递归的解法int editDistanceIteratively(string source, string target){int len_source = source.length();int len_target = target.length();// test for degenerate cases of empty string of source or target stringif(len_source == 0)return len_target;if(len_target == 0)return len_source;// do initilizationint** dMatrix = new int*[len_source+1];for(int i = 0; i <= len_source; i++){dMatrix[i] = new int[len_target+1];memset(dMatrix[i], 0, sizeof(int)*(len_target+1));}for(int i = 1; i <= len_source; i++)dMatrix[i][0] = i;for(int i = 1; i <= len_target; i++)dMatrix[0][i] = i;for(int j = 1; j <= len_target; j++)for(int i = 1; i <= len_source; i++){if(source[i-1] == target[j-1]){dMatrix[i][j] = dMatrix[i-1][j-1];}else{dMatrix[i][j] = minimum(dMatrix[i-1][j]+1, dMatrix[i][j-1]+1, dMatrix[i-1][j-1]+1);}}return dMatrix[len_source][len_target];}
- 百度-20120926-字符串编辑距离
- 字符串的编辑距离
- 字符串编辑距离查表法
- 求字符串编辑距离
- 字符串的编辑距离
- 字符串编辑距离
- 字符串编辑距离
- 1020 字符串编辑距离
- 字符串编辑距离
- 字符串编辑距离
- 字符串编辑距离
- 字符串编辑距离
- 求字符串编辑距离
- 字符串编辑距离
- 字符串编辑距离算法
- 编辑字符串距离
- 字符串编辑距离
- 字符串编辑距离
- Jenkins安装及问题
- TQ210搭载Android4.0.3系统构建之LED从驱动到HAL到JNI到应用程序(JNI篇)
- C语言位运算详解
- 关于谷歌地图
- BlazeDS详解
- 百度-20120926-字符串编辑距离
- Openfire中的基本实体及关系
- 数据仓库与数据集市
- ASP.NET前台代码绑定后台变量方法总结
- H264 TS/ES
- TQ210搭载Android4.0.3系统构建之LED从驱动到HAL到JNI到应用程序(应用程序篇)
- 黑马程序员——java第三天:基础语法(三元运算符,while、switch、for语句)
- python模块之xml.etree.ElementTree
- 学习中,写的第一个shell