行编辑距离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 所使用的最少操作数 . 你可 ...
随机推荐
- POI/Excel/HTML单元格公式问题
一.问题描述 使用MyBatis从数据库中获取数据,然后用POI把数据填充到Excel模板中,生成最终的xls文件.把最终的xls文件转换为html文件,并返回给前台显示在Panel中. Excel模 ...
- 【Win 10 应用开发】UI Composition 札记(四):绘制图形
使用 Win 2D 组件,就可以很轻松地绘制各种图形,哪怕你没有 D2D 相关基础,也不必写很复杂的 C++ 代码. 先来说说如何获取 Win 2D 组件.很简单,创建 UWP 应用项目后,你打开“解 ...
- python的学习之路day1
软件:python3.pycharm开发工具 python的开始:print("hello world") 注意:python3需要加上() 1.变量是什么:在程序运行过程中它的值 ...
- 【深度学习笔记】(二)基于MNIST数据集的神经网络实验
一.介绍 MNIST(Mixed National Institute of Standards and Technology database)是网上著名的公开数据库之一,是一个入门级的计算机视觉数 ...
- 为JS内置对象添加常用方法
1.字符串全部替换: String.prototype.replaceAll = function(s1,s2){ return this.replace(new RegExp(s1,"gm ...
- STM32基础问题分析——PWM配置
STM32基础问题分析--PWM配置 在使用STM32F103产生固定频率.固定占空比的PWM波时,虽然有官方以及众多开发板提供的例程,但是关于有点问题并没有说的很清晰,并且<STM32F10X ...
- Vim配置及使用笔记
Vim配置及使用笔记 安装 apt-get install vim -y 配置说明 vim /etc/vim/vimrc 在配置文件后加入这些配置项 set nu set tabstop=4 set ...
- abstract的方法是否可同时是static,是否可同时是native,是否可同时是synchronized?
1.abstract与static (what) abstract:用来声明抽象方法,抽象方法没有方法体,不能被直接调用,必须在子类overriding后才能使用 static:用来声明静态方法,静态 ...
- 【Java】单词倒序输出
如何将一段单词倒序输出?把"Hello Java Hello China"变成"China Hello Java Hello"? 看起来好像很简单,只需要把字符 ...
- Python 日志处理(一) 按Nginx log_format 分割日志记录
要求:不使用正则 根据nginx 默认的日志记录格式,分割日志记录. log_format main '$remote_addr - $remote_user [$time_local] " ...