edit-distance-动态规划,计算两词之间变换的最小步数
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-动态规划,计算两词之间变换的最小步数的更多相关文章
- edit distance(编辑距离,两个字符串之间相似性的问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Edit Distance问题在两种编程范式下的求解
本文已授权 [Coding博客](https://blog.coding.net) 转载 前言 Edit Distance,中文叫做编辑距离,在文本处理等领域是一个重要的问题,以下是摘自于百度百科的定 ...
- java:通过Calendar类正确计算两日期之间的间隔
在开发Android应用时偶然需要用到一个提示用户已用天数的功能,从实现上来看无非就是持久化存入用户第一次使用应用的时间firstTime(通过SharedPreferences .xml.sqlit ...
- Vector3函数理解-计算两向量之间的角度
1.已知两个向量dirA,dirB.Vector3 dirA = new Vector3(-1,1,0); Vector3 dirB = new Vector3(-1,1,1);2.使向量处于同一个平 ...
- Word Ladder II——找出两词之间最短路径的所有可能
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 行编辑距离Edit Distance——动态规划
题目描写叙述: 给定一个源串和目标串.可以对源串进行例如以下操作: 1. 在给定位置上插入一个字符 2. 替换随意字符 3. 删除随意字符 写一个程序.返回最小操作数,使得对源串进行这些操作后等 ...
- C++练习 | 计算两日期之间天数差
#include<iostream> #include<string> #include<cstring> using namespace std; class D ...
- Edit Distance(动态规划,难)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- js计算两经纬度之间的距离
js如下: // 方法定义 lat,lng function GetDistance( lat1, lng1, lat2, lng2){ var radLat1 = lat1*Math.PI / ...
随机推荐
- android之卸载反馈的功能
感谢这位大神:http://www.eoeandroid.com/thread-317728-1-1.html zip包里面有讲解的试用方法,和如何试用ndk编译的方法,本人亲身试验,确实可用,现做一 ...
- SqlServer 还原数据库 代码实现
RESTORE DATABASE TargetDB FROM DISK = 'D:\DataBase\DB.bak' with replace, MOVE 'DB' TO 'D:\DataBase\T ...
- 《图解CSS3:核心技术与案例实战》
<图解CSS3:核心技术与案例实战> 基本信息 作者: 大漠 丛书名: Web开发技术丛书 出版社:机械工业出版社 ISBN:9787111469209 上架时间:2014-7-2 出版日 ...
- 《嵌入式Linux开发实用教程》
<嵌入式Linux开发实用教程> 基本信息 作者: 朱兆祺 李强 袁晋蓉 出版社:人民邮电出版社 ISBN:9787115334831 上架时间:2014-2-13 出版日期: ...
- 利用Logstash插件进行Elasticsearch与Mysql的数据
Logstash与Elasticsearch的安装就不多说了,我之前有两篇文章写的比较详细了ElasticSearch + Logstash + Kibana 搭建笔记 和 Filebeat+Logs ...
- 简单实现网页另存为word或者excel
最近在弄网页的导出,在网上找了很多的方法,最后用一种较为简单的方法实现,这次的寻找和解决问题的过程使我加深了对封装和面向对对象的理解更加深刻!在B/S阶段,有很多已经封装好的类,而且在所有的页面都是一 ...
- [leetcode]Best Time to Buy and Sell Stock III @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意: Say you have an array ...
- Backbone.js 的 View 中定义事件
使用 Backbone 的 View 时,可以象传统 jQuery 那样定义事件,$("selector").click(function(){...}).幸运的是 Backbon ...
- mahout安装
mahout是hadoop的一种高级应用.运行mahout需要提前安装好hadoop.hadoop的安装网上很多.而且也不复杂,这里不再讲述.这里默认hadoop已经安装完成. 1:下载二进制解压安装 ...
- 平均值(Mean)、方差(Variance)、标准差(Standard Deviation) (转)
http://blog.csdn.net/xidiancoder/article/details/71341345 平均值 平均值的概念很简单:所有数据之和除以数据点的个数,以此表示数据集的平均大小: ...