题目描写叙述:

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

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. 获取 修改 CSS 样式

    内联(style里的)样式 element.style.color element.style.getPropertyValue("color")   非内联样式 window.g ...

  2. 百度Echarts导入

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. POI/Excel/HTML单元格公式问题

    一.问题描述 使用MyBatis从数据库中获取数据,然后用POI把数据填充到Excel模板中,生成最终的xls文件.把最终的xls文件转换为html文件,并返回给前台显示在Panel中. Excel模 ...

  4. Vue.js用法详解(一)更新中~

      前  言 前段时间为了一个数据查询的项目自学了Vue,感觉这款框架还是很不错的,今天就整理整理这个框架如何使用,希望对正在学这个框架的小伙伴有所帮助~ 首先,我们先来了解一下Vue: Vue.js ...

  5. Oracle的Recyclebin策略

    1.从oracle10g开始删除数据库表的时候并不是真正删除,而是放到了recyclebin中,这个过程类似 windows里面删除的文件会被临时放到回收站中. 2.删除的表系统会自动给他重命名就是你 ...

  6. axios在vue中的简单配置与使用

    一.axios 简介 axios 是一个基于Promise 用于浏览器和 nodejs 的 HTTP 客户端,它本身具有以下特征:https://hzzly.github.io/2017/03/12/ ...

  7. maven学习之二

    三 profile介绍 可以有多个地方定义profile.定义的地方不同,它的作用范围也不同. (1)    针对于特定项目的profile配置我们可以定义在该项目的pom.xml中. (2)     ...

  8. LKD: Chapter 8 Bottom Halves and Deferring Work

    In 2.6.x, there are 3 mechanisms for implementing a bottom half: softirqs, tasklets and work queues. ...

  9. 使用face_recognition批量识别图片中的人数

    #使用face_recognition实现从图片中选中人数并分别输出txtimport face_recognition import cv2 import os fin = 'D:\\Users\\ ...

  10. Bin、App_Data等文件夹详述(转自http://blog.csdn.net/zzjiadw/article/details/6801506)

    ASP.NET应用程序和ASP.Net网站所共有的文件: App_Browsers 包含 ASP.NET 用于标识个别浏览器并确定其功能的浏览器定义 (.browser) 文件.有关更多信息,请参见浏 ...