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 / ...
随机推荐
- MySql和相关驱动的安装方式
下载mySql for java驱动的地址:http://www.mysql.com/products/connector/ (可下可不下,因为安装mySql的时候就会包含了各种驱动) MySQL下载 ...
- 使用Python读取Mp3的标签信息
什么是ID3 MP3是音频文件最流行的格式,它的全称是 MPEG layer III.但是这种格式不支持对于音频内容的描述信息,包括歌曲名称.演唱者.专辑等等. 因此在1996年,Eric Kemp在 ...
- Visual Studio Code compile error - launch.json must be configured...
一.在最初使用VS Code创建控制台应用时, 使用VS Code调试工具默认会跑出异常: launch: program 'launch: launch.json must be configure ...
- request.getParameter();的意思
对于httprequrest的request.getParameter()的作用,之前我只是在用它而不知道它到底有什么作用,今天看了一遍文章突然明白了其中的意思. 大致的内容如下: <form ...
- [转]PHP中替换换行符
FROM :http://www.cnblogs.com/siqi/archive/2012/10/12/2720713.html //php 有三种方法来解决 //1.使用str_replace 来 ...
- 前端基于jquery的UI框架
正在做的一个项目选择jquery作为前端js核心库.然后就想选一个基于jquery的ui库,然后悲催的事情发生了. 至于为什么使用jquery,一是因为不想为授权费用,而又不想引起可能法律纠纷:另一方 ...
- python机器学习sklearn 岭回归(Ridge、RidgeCV)
1.介绍 Ridge 回归通过对系数的大小施加惩罚来解决 普通最小二乘法 的一些问题. 岭系数最小化的是带罚项的残差平方和, 其中,α≥0α≥0 是控制系数收缩量的复杂性参数: αα 的值越大,收缩量 ...
- 关于UITableView的黑线条
这里採用分组形式.底色可用图片覆盖,设置为cell(是一个view)的子视图,并把cell'的颜色设置为底色 当UITableView的cell没有满屏显示时,就会出现黑线条.我们仅仅需在初始化时,加 ...
- ML&DL视频教程资源
作者:Bruce链接:https://www.zhihu.com/question/49909565/answer/345894856来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...
- JavaSE(十)之Map总结 (转)
http://www.cnblogs.com/zhangyinhua/p/7545979.html 阅读目录(Content) 一.Map接口 1.1.为什么Collection不能满足集合的所有功能 ...