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. 美国恐怖故事第一季/全集American Horror Story 1全迅雷下载

    第一季 American Horror Story Season 1 (2011)看点:心理治疗师Ben(迪伦·麦克德莫特 Dylan McDermott 饰)因与女学生有染被妻子Vivien(康妮· ...

  2. Android手机提示“未安装应用程序”

    用eclipse调试应用时,遇到了这个问题,网上给出的解决方案倒是挺多,但似乎一个都没奏效,而且我手机也重启了,还是有问题,郁闷ing-   然后看到一篇文章指出,可能不是签名和SD的卡问题,而是我们 ...

  3. ios成长之每日一遍(day 7)

    今天到UITabBarController 结合 UIPickView, 这里一共有5个实现, 由浅到易. 其实在IB上面使用UITabBarController很简单, 就像平常拖控件一样拖到界面上 ...

  4. 【转】内存分析工具 MAT 的使用

    本文转载自:http://blog.csdn.net/aaa2832/article/details/19419679 1 内存泄漏的排查方法 Dalvik Debug Monitor Server ...

  5. SearchView的最简单的使用方式

    SearchView顾名思义就是一个搜索视图,和之前讲解的自动匹配的输入框类似.只不过他有自己特有的监听器,并且可以实时得到用户输入的结果.对于SearchView这个控件,我强烈建议将其放在Acti ...

  6. KOL:Key Opinion Leader

    什么是关键意见领袖 在营销学上,为各厂家宣传的专家或权威被称为“关键意见领袖(Key Opinion Leader),通常被定义为:拥有更多.更准确的产品信息,且为相关群体所接受或信任,并对该群体的购 ...

  7. [转]php判断一个数组是另一个数组的子集

    FROM : http://blog.csdn.net/lcion/article/details/8985220 今天完成一个算法的过程中,有几个需求模块,其中就有判断$a数组是否是$b数组的子集, ...

  8. 在Visual Studio 2013里使用 Microsoft SQL Server Compact Edition

    (1)下载SQLCE组件 https://www.microsoft.com/en-us/download/details.aspx?id=17876 (2)下载VS2013工具设计器 https:/ ...

  9. Java NIO和IO的区别(转)

    原文链接:Java NIO和IO的区别 下表总结了Java NIO和IO之间的主要差别,我会更详细地描述表中每部分的差异. 复制代码代码如下: IO                NIO面向流     ...

  10. 用EntityFramework6连接MySQL【转】

    http://www.cnblogs.com/wujingtao/p/5399950.html 不是微软的亲儿子这待遇就是不一样,其中的坑可真实不少,第一次连MySQL足足折腾了我大半天. 废话不多说 ...