行编辑距离Edit Distance——动态规划
题目描写叙述:
给定一个源串和目标串。可以对源串进行例如以下操作:
1. 在给定位置上插入一个字符
2. 替换随意字符
3. 删除随意字符
写一个程序。返回最小操作数,使得对源串进行这些操作后等于目标串,源串和目标串的长度都小于2000。
思路:
设状态dp[i][j] 表示从源串s[0...i] 和 目标串t[0...j] 的最短编辑距离
边界为:dp[i][0] = i,dp[0][j] = j
递推方程:
- 假设s[i] == t[j], 那么 dp[i][j] = dp[i-1][j-1]
- 假设s[i] != t[j],那么有三种操作情况:
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——动态规划的更多相关文章
- 利用编辑距离(Edit Distance)计算两个字符串的相似度
利用编辑距离(Edit Distance)计算两个字符串的相似度 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可 ...
- 编辑距离——Edit Distance
编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...
- [Leetcode 72]编辑距离 Edit Distance
[题目] Given two words word1 and word2, find the minimum number of operations required to convert word ...
- [Swift]LeetCode72. 编辑距离 | Edit Distance
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- Edit Distance(动态规划,难)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 编辑距离Edit Distance 非常典型的DP类型题目
https://leetcode.com/problems/edit-distance/?tab=Description 真的非常好,也非常典型. https://discuss.leetcode.c ...
- 动态规划 求解 Minimum Edit Distance
http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)
Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...
随机推荐
- svg snap 笔记
路径中的字母,大写相对于左上角绝对定位,小写相对定位 M110,95,95,110M115,100,100,115 pattern 类似于图片拼贴,可以把指定位置的图案用来填充 var patt ...
- 用户需求与NABCD分析
用户需求与NABCD分析 目录 项目简介 用户需求分析 调研途径 问卷情况说明 问卷反馈与分析 NABCD分析 Need 需求 Approach 途径 Benefit 好处 Competitors 竞 ...
- 【APP问题定位(三)】adb安装
先来剧透一下我们需要使用的工具 bin包 一个安装目录,可以免安装直接调用adb命令 Android SDK platform tools 下面依次为大家介绍,第1个和第2 ...
- Get started with Google Analytics
What is Google Analytics Google Analytics is a Google official analytics tool that is primarily used ...
- 《天书夜读:从汇编语言到windows内核编程》一 汇编指令与C语言
1. Debug模式下,VC++6.0下断点运行,按CTRL+F11可查看汇编代码:另外可以用cl /c /FAs YourCppFile.cpp命令行在同目录生成YourCppFile.asm汇编文 ...
- asp.net web api客户端调用
服务接口 接口1: //Post:http://127.0.0.1/HY_WebApi/api/V2/Key/FunctionTest1 [HttpPost] public HttpResponseM ...
- CLR之委托的揭秘(一)
初识委托: 在之前的学习中我们已经可以把对象,值,数组当作参数传递给方法,但是有没有可能把方法也当作参数传递给方法呢?有了这个想法于是就有了委托.方法当作一种参数去传递,但是方法有的 ...
- 对java泛型的理解
正确的应用java泛型的特性可以更好的实现编程的开闭原则(对扩展开放,对修改关闭),这得益于java泛型提供的在程序运行时获取对象声明类型的特性. 静态语言的特性是在程序编译前进行声明,这样程序在编译 ...
- 下一个计划 : .NET/.NET Core应用性能管理系统
前言 最近几个月一直在研究开源的APM和监控方案,并对比使用了Zipkin,CAT,Sky-walking,PinPoint(仅对比,未实际部署),Elastic APM,TICK Stack,Pro ...
- Dynamics 365创建电子邮箱字段包含值的联系人同时更改负责人的方法。
摘要: 本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复267或者20171129可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyon ...