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 / ...
随机推荐
- epoll的两种工作模式
epoll有两种模式,Edge Triggered(简称ET) 和 Level Triggered(简称LT).在採用这两种模式时要注意的是,假设採用ET模式,那么仅当状态发生变化时才会通知,而採用L ...
- [Nginx] Nginx 配置location总结
cp from : https://www.cnblogs.com/coder-yoyo/p/6346595.html location匹配顺序 "="前缀指令匹配,如果匹配成功, ...
- 模型标准化——预测模型标记语言(PMML)
https://www.cnblogs.com/pinard/p/9220199.html 在机器学习用于产品的时候,我们经常会遇到跨平台的问题.比如我们用Python基于一系列的机器学习库训练了一个 ...
- SKU与SPU
首先,搞清楚商品与单品的区别.例如,iphone是一个单品,但是在淘宝上当很多商家同时出售这个产品的时候,iphone就是一个商品了. 商品:淘宝叫item,京东叫product,商品特指与商家有关的 ...
- 一种开源的分布式消息系统Nats
一种开源的分布式消息系统Nats 作者:chszs.未经博主同意不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs 1.NATS介绍 NATS是一个开源的 ...
- Swift3.0:照片选择
一.介绍 图片选择或者拍照功能: 1.选择相册中的图片或是拍照,都是通过UIImagePickerController控制器实例化一个对象,然后通过self.presentViewController ...
- 初识EntityFramework6【转】
http://www.cnblogs.com/wujingtao/p/5401132.html 什么是EF? EF是一种ORM(Object-relational mapping)框架,它能把我们在编 ...
- Windows server 2008 SSD性能测试
过渡到windows 7.windows8是趋势,老迈的windows xp .windows server 2003已经快到淘汰的阶段,安装了windows server 2008 R2 ,测试了下 ...
- Java方法containsAll学习
有时候我们需要判断B链表是不是A链表的子集,我们可以使用A.containsAll(B)来判断,当返回值是true的时候就表明B链表是A链表的子集,当返回值是false时候就表明B链表不是A链表的子集 ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(四)针对hadoop2.9.0启动执行start-all.sh出现异常:failed to launch: nice -n 0 /bin/spark-class org.apache.spark.deploy.worker.Worker
启动问题: 执行start-all.sh出现以下异常信息: failed to launch: nice -n 0 /bin/spark-class org.apache.spark.deploy.w ...