Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

a) Insert a character
b) Delete a character
c) Replace a character

动态规划,使用数组记录每一步的步数,注意循环长度为<=length,dp[0][0]表示null的时候

class Solution {
public:
int minDistance(string word1, string word2) {
if(word1.empty())
return word2.length();
if(word2.empty())
return word1.length();
int dp[word1.length()+][word2.length()+];
dp[][]=;
int i,j;
for(i=;i<=word1.length();++i)
dp[i][]=i;
for(j=;j<=word2.length();++j)
dp[][j]=j;
for(i=;i<=word1.length();++i)
{
for(j=;j<=word2.length();++j)
{
if(word1[i-]==word2[j-])
dp[i][j]=dp[i-][j-];
else
dp[i][j]=min(min(dp[i][j-],dp[i-][j]),dp[i-][j-])+;
}
}
return dp[word1.length()][word2.length()];
}
};

edit-distance-动态规划,计算两词之间变换的最小步数的更多相关文章

  1. edit distance(编辑距离,两个字符串之间相似性的问题)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  2. Edit Distance问题在两种编程范式下的求解

    本文已授权 [Coding博客](https://blog.coding.net) 转载 前言 Edit Distance,中文叫做编辑距离,在文本处理等领域是一个重要的问题,以下是摘自于百度百科的定 ...

  3. java:通过Calendar类正确计算两日期之间的间隔

    在开发Android应用时偶然需要用到一个提示用户已用天数的功能,从实现上来看无非就是持久化存入用户第一次使用应用的时间firstTime(通过SharedPreferences .xml.sqlit ...

  4. Vector3函数理解-计算两向量之间的角度

    1.已知两个向量dirA,dirB.Vector3 dirA = new Vector3(-1,1,0); Vector3 dirB = new Vector3(-1,1,1);2.使向量处于同一个平 ...

  5. Word Ladder II——找出两词之间最短路径的所有可能

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  6. 行编辑距离Edit Distance——动态规划

    题目描写叙述: 给定一个源串和目标串.可以对源串进行例如以下操作:  1. 在给定位置上插入一个字符  2. 替换随意字符  3. 删除随意字符 写一个程序.返回最小操作数,使得对源串进行这些操作后等 ...

  7. C++练习 | 计算两日期之间天数差

    #include<iostream> #include<string> #include<cstring> using namespace std; class D ...

  8. Edit Distance(动态规划,难)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  9. js计算两经纬度之间的距离

    js如下: // 方法定义 lat,lng function GetDistance( lat1, lng1, lat2, lng2){    var radLat1 = lat1*Math.PI / ...

随机推荐

  1. 血族第四季/全集The Strain迅雷下载

    当第四季开始时,故事时间已经过去九个月.世界陷入黑暗,斯特里高伊吸血鬼控制了一切.第三季结尾的爆炸引发了一场全球核灾难,核冬天的到来令地表变得暗无天日,斯特里高伊获得解放.它们大白天也能出来活动,帮助 ...

  2. ack-grep 代码全文搜索

    安装 ubuntu下要安装ack-grep,因为在debian系中,ack这个名字被其他的软件占用了. sudo apt-get install ack-grep 特点 大家都说自己的东西好,因此ac ...

  3. EditText 限制输入,自定义样式,监听输入的字符,自动换行

    自动获取焦点 <!-- 添加:<requestFocus /> 会自动获取焦点 --> <EditText android:layout_width="matc ...

  4. 内存数据库-H2简介与实践

    一.H2数据库介绍 H2数据库地址:http://www.h2database.com/html/main.html H2是一个开源的嵌入式(非嵌入式设备)数据库引擎,它是一个用Java开发的类库,可 ...

  5. C#中byte[] 转 double[] 或 int[] 或 struct结构体

    方法:使用C#调用C++ memcpy实现各种参数类型的内存拷贝 using System.Runtime.InteropServices; public class GlbWSGridDataset ...

  6. Server 2008 R2大改造变成梦幻Win7系统

    在此之前先补充一下知识Windows Server 2008和Windows Server 2008 R2的不同之处Windows Server 2008是基准与Vista的内核构建的,支持X86框架 ...

  7. JavaScript递归方法 生成 json tree 树形结构数据

    //递归方法 生成 json tree 数据 var getJsonTree = function(data, parentId) { var itemArr = []; for (var i = 0 ...

  8. 不得不知的ES6十大特性

    ES6(ECMAScript2015)的出现,无疑给前端开发人员带来了新的惊喜,它包含了一些很棒的新特性,可以更加方便的实现很多复杂的操作,提高开发人员的效率. 本文主要针对ES6做一个简要介绍. 主 ...

  9. MFC中打印对话框CPrintDialog类

    void CCPrintDialogView::OnPrint() { DWORD dwflags=PD_ALLPAGES|PD_NOPAGENUMS|PD_USEDEVMODECOPIES|PD_S ...

  10. 推荐系统resys小组线下活动见闻2009-08-22

    http://www.tuicool.com/articles/vUvQVn 时间2009-08-30 15:13:22  不周山原文  http://www.wentrue.net/blog/?p= ...