题目描写叙述:

给定一个源串和目标串。可以对源串进行例如以下操作: 

1. 在给定位置上插入一个字符 

2. 替换随意字符 

3. 删除随意字符

写一个程序。返回最小操作数,使得对源串进行这些操作后等于目标串,源串和目标串的长度都小于2000。

思路:

设状态dp[i][j] 表示从源串s[0...i] 和 目标串t[0...j] 的最短编辑距离

边界为:dp[i][0] = i,dp[0][j] = j

递推方程:

  1. 假设s[i] == t[j], 那么 dp[i][j] = dp[i-1][j-1]
  2. 假设s[i] != t[j],那么有三种操作情况:
将s[i]删除。dp[i][j] = dp[i-1][j] + 1;
将s中加入t[j],dp[i][j] = dp[i][j-1] +1;
将s和t进行替换,dp[i][j] = dp[i-1][j-1] +1。

因此,能够写出状态转移方程:
dp[i][j] = min(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1] + (s[i]==t[j] ? 0 :1))
分别相应:删除、加入、替换(若相等就不替换)

代码:

class Solution {
public:
int minDistance(string word1, string word2) {
int Slen = word1.size();
int Tlen = word2.size();
int dp[Slen+1][Tlen+1] = {0};//注意:这里都+1,而且初始化为0
//长度为n的字符串有n+1个隔板
for(int i=1; i<=Slen; i++) //注意从1開始
dp[i][0] = i;
for(int j=1; j<=Tlen; j++)
dp[0][j] = j;
for(int i=1; i<=Slen; i++)
{
for(int j=1; j<=Tlen; j++)
{
if(word1[i-1] == word2[j-1])
dp[i][j] = dp[i-1][j-1];
else
{
int temp = min(dp[i-1][j], dp[i][j-1]);
dp[i][j] = min(temp, dp[i-1][j-1]) + 1;
}
}
}
return dp[Slen][Tlen];
}
};





行编辑距离Edit Distance——动态规划的更多相关文章

  1. 利用编辑距离(Edit Distance)计算两个字符串的相似度

    利用编辑距离(Edit Distance)计算两个字符串的相似度 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可 ...

  2. 编辑距离——Edit Distance

    编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...

  3. [Leetcode 72]编辑距离 Edit Distance

    [题目] Given two words word1 and word2, find the minimum number of operations required to convert word ...

  4. [Swift]LeetCode72. 编辑距离 | Edit Distance

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  5. Edit Distance(动态规划,难)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  6. 编辑距离Edit Distance 非常典型的DP类型题目

    https://leetcode.com/problems/edit-distance/?tab=Description 真的非常好,也非常典型. https://discuss.leetcode.c ...

  7. 动态规划 求解 Minimum Edit Distance

    http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...

  8. leetCode 72.Edit Distance (编辑距离) 解题思路和方法

    Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert  ...

  9. Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)

    Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...

随机推荐

  1. Problem C: 求个最大值

    class MaxValue { public: vector<int> vec; void append(int n) { vec.push_back(n); } int getMax( ...

  2. Apple 公司开发者账号添加团队成员

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  3. java面试题及答案

    JAVA相关基础知识1.面向对象的特征有哪些方面 1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时 ...

  4. Python待分析的模块

    fcntl 文件控制模块 http://docs.python.org/2.7/library/fcntl.html#module-fcntl struct 二进制文本处理模块 http://docs ...

  5. [转]开源框架完美组合之Spring.NET + NHibernate + ASP.NET MVC + jQuery + easyUI 中英文双语言小型企业网站Demo

    热衷于开源框架探索的我发现ASP.NET MVC与jQuery easyUI的组合很给力.由于原先一直受Ext JS框架的licence所苦恼,于是痛下决心寻找一个完全免费的js框架——easyUI. ...

  6. CSS实现盒子高度撑开且以最高的为高

    前端开发中,常常会有需求两个盒子并排排列,高度以最高的为准,且高度是内容撑开的,类似于这样 如果不是用 table 布局,而是用 div 布局,两个子盒子浮动来实现的话,实际上默认写出来是这样的 此时 ...

  7. Elasticsearch6.0及其head插件安装

    Elasticsearch6.0及其head插件安装 1.下载并解压elasticsearch 2.修改elasticsearch.yml文件 # 集群的名字 cluster.name: my-app ...

  8. 【续】抓个Firefox的小辫子,jQuery表示不背这黑锅,Chrome,Edge,IE8-11继续围观中

    引子 昨天我发了一篇文章[抓个Firefox的小辫子,围观群众有:Chrome.Edge.IE8-11],提到了一个Firefox很多版本都存在的问题,而相同的测试页面在Chrome.Edge.IE8 ...

  9. 爱立信开始大规模mesh网络测试

    mesh网络可谓是物联网之关键,相较于传统有线技术,无线连接的mesh网络实施成本较低,而且更具有适应性和可扩展性,让高通信量的应用更加可靠. 虽然Thread和ZigBee等细分技术也能提供标准化的 ...

  10. php数据分页显示基础

    一:分页原理: 所谓分页显示,也就是将数据库中的结果集认为的分成一段一段的来显示,需要两个初始的参数: 每页多少条记录 ($PageSize)? 当前是第几页($CurrentPageID)? 还有其 ...