Edit Distance
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
分析:
处理这道题也是用动态规划。
动态数组dp[word1.length+1][word2.length+1]
dp[i][j]表示从word1前i个字符转换到word2前j个字符最少的步骤数。
假设word1现在遍历到字符x,word2遍历到字符y(word1当前遍历到的长度为i,word2为j)。
以下两种可能性:
1. x==y,那么不用做任何编辑操作,所以dp[i][j] = dp[i-1][j-1]
2. x != y
(1) 在word1插入y, 那么dp[i][j] = dp[i][j-1] + 1
(2) 在word1删除x, 那么dp[i][j] = dp[i-1][j] + 1
(3) 把word1中的x用y来替换,那么dp[i][j] = dp[i-1][j-1] + 1
最少的步骤就是取这三个中的最小值。
最后返回 dp[word1.length+1][word2.length+1] 即可。
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.size();
int n = word2.size();
vector<vector<int>> dp(m+);
for(size_t i = ; i<m+; i++)
for(size_t j = ; j<n+; j++)
dp[i].push_back(-);
for(size_t i=; i<m+;i++)
dp[i][] =i;
for(size_t j=; j<n+; j++)
dp[][j] =j;
for(int i=; i<m+; i++)
for(int j =; j<n+; j++)
{
if(word1[i-] == word2[j-]){
dp[i][j] = dp[i-][j-];
}
else{
int insert = dp[i-][j]+;
int replace = dp[i-][j-]+;
int del = dp[i][j-]+;
dp[i][j] = min(del,min(insert, replace));
}
}
return dp[m][n];
}
};
Edit Distance的更多相关文章
- [LeetCode] One Edit Distance 一个编辑距离
Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 编辑距离——Edit Distance
编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...
- LintCode Edit Distance
LintCode Edit Distance Given two words word1 and word2, find the minimum number of steps required to ...
- stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)
I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- 动态规划 求解 Minimum Edit Distance
http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...
- One Edit Distance
Given two strings S and T, determine if they are both one edit distance apart. 分析:https://segmentfau ...
- 【leetcode】Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
随机推荐
- Mac上编译C++报错
今天在使用Mac编译C++文件时,提示以下错误. Undefined symbols for architecture x86_64: "std::__1::__vector_base_co ...
- 24、ASP.NET MVC入门到精通——数据库仓储
本系列目录:ASP.NET MVC4入门到精通系列目录汇总 业务层调用数据层对象,我不想每次都new一个数据层对象,而是在数据层创建一个仓储,统一管理所有的对象调用. 1.在IDAL项目中,新建IDB ...
- 1 为什么搭建.Net core下的云开发框架
几年前我组织开发了综合业务管理系统,该系统包含系统门户.业务信息.联系处置.数据查询.指标报表等功能板块,其中涵盖了门户定制.工作流引擎.自定义表单.指标计算.通用数据展示.通用后台服务.用户授权认证 ...
- CSS笔记之伪类与伪元素
伪类分为两种:UI伪类 与 结构化伪类 UI伪类:a:link{} a:hover{} a:active{} a:visited{} input[type='text']:focus{} ...
- Thinkcmf:页面常用函数
Thinkcmf:页面常用函数 全站seo: 文章列表: {$site_seo_title} <!--SEO标题--> {$site_seo_keywords} < ...
- jquery键盘事件总结
在工作中在发现同事在写输入密码按键的相关js效果时,发现自己对于这块很是不了解,这几天特地了解了一下,进行以下总结: 一.首先要知道键盘事件的几个属性: 1.keydown():在键盘按下时触发. 2 ...
- iOS--xuer(registration)
这个登录页面包含了自适应屏幕的大小,数字用户登录键盘是数字键盘.隐藏键盘.隐藏密码等等. ViewController.h #import <UIKit/UIKit.h> #import ...
- IOS开发之Bug--iOS7View被导航栏遮挡问题的解决
在实际开发中,遇到在UITextView的frame等于当前控制器的View的frame的情况下,然后运行的时候,发现控制器的Frame的高度y值会从导航条的位置64变化到0. 导致UITextVie ...
- C#winfrom播放器动态加载歌词
上周我们进行了结业项目答辩,是播放器项目.有一个关于播放器变唱歌边加载歌词的方法特别有意思,像酷狗那样子歌词和歌曲同步滚播的样子. 这里的工具是Visual Studio 2013,使用语言是C#和. ...
- Elasticsearch-HttpServerModule
HttpServerModule的请求主要由HttpServer中的HttpServerTransport(默认为NettyHttpServerTransport)类处理. NettyHttpServ ...