动态规划——Edit Distance
Example 1:
Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')
Example 2:
Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')
状态转移方程:
(2)如果word1[i-1] != word2[j-1],由于没有一个特别有规律的方法来断定执行何种操作,在增加、删除、替换三种操作中选一种操作次数少的赋值给dp[i][j];
增加操作:dp[i][j] = dp[i][j-1] + 1
删除操作:dp[i][j] = dp[i-1][j] + 1
int minDistance(string word1,string word2){
int wlen1 = word1.size();
int wlen2 = word2.size();
int**dp = new int*[wlen1 + ];
for (int i = ; i <= wlen1; i++)
dp[i] = new int[wlen2 + ];
//int dp[maxn][maxn] = { 0 };
for (int i = ; i <= wlen1; i++)
dp[i][] = i;
for (int j = ; j <= wlen2; j++)
dp[][j] = j;
int temp = ;
for (int i = ; i <= wlen1; i++){
for (int j = ; j <= wlen2; j++){
if (word1[i - ] == word2[j - ])dp[i][j] = dp[i - ][j-];
else{
temp = dp[i - ][j - ]<dp[i - ][j] ? dp[i - ][j - ] : dp[i - ][j];
temp = temp < dp[i][j - ] ? temp : dp[i][j - ];
dp[i][j] = temp + ;
}
}
}
/*
for (int i = 0; i <= wlen1; i++)
delete[]dp[i];
delete[]dp;
*/
return dp[wlen1][wlen2];
}
动态规划——Edit Distance的更多相关文章
- 动态规划 求解 Minimum Edit Distance
http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)
Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...
- Edit Distance——经典的动态规划问题
题目描述Edit DistanceGiven two words word1 and word2, find the minimum number of steps required to conve ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 编辑距离——Edit Distance
编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...
- 【leetcode】Edit Distance
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- 56. Edit Distance && Simplify Path
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
随机推荐
- python之路day06--python2/3小区别,小数据池的概念,编码的进阶str转为bytes类型,编码和解码
python2#print() print'abc'#range() xrange()生成器#raw_input() python3# print('abc')# range()# input() = ...
- ElasticSearch6.5.0 【Java客户端之REST Client】
说明 High Level Client 是基于 Low Level Client 的.官方文档如下: * https://www.elastic.co/guide/en/elasticsearch/ ...
- JS学习笔记Day9
一.BOM (一)概念:是 Browser object model 的缩写,简称浏览器对象模型. BOM 提供了独立于内容而与浏览器窗口进行交互的对象 由于 BOM 主要用于管理窗口与窗口之间的通讯 ...
- Pandas系列(八)-筛选工具介绍
内容目录 1. 字典式 get 访问 2. 属性访问 3. 切片操作 4. 通过数字筛选行和列 5. 通过名称筛选行和列 6. 布尔索引 7. isin 筛选 8. 通过Callable筛选 数据准备 ...
- 金融量化分析【day110】:NumPy多维数组
一.Numpy简介 NumPy 是高性能科学计算和数据分析的基础包,它是pandas等其他各种工具的基础 1.主要功能 1.ndarray,一个多维数组结构,高效且节省空间 2.无序循环对整组数据进行 ...
- jQuery使用(十三):工具方法
proxy() onConflict() each() map() parseJson() makeArray() proxy() $.proxy()的实现机制与原生javaScript中的bind( ...
- SpringBoot系列: CommandLineRunner接口的用处
========================================使用 CommandLineRunner 对Spring Bean进行额外初始化==================== ...
- git 20181119
不同分支间(a.b分支)合并部分文件 git ck b git ck a f1 f2 git配置 配置文件 系统git config --system --list 全局git config --gl ...
- NB卡开卡注意事项【转】
转自:https://blog.csdn.net/cheng401733277/article/details/83276436 版权声明:本文为博主原创文章,未经博主允许不得转载. https:// ...
- python3编程练习题
记录一些基础编程练习题和一些遇见的坑 1)输出0到100的数字,如果数字是3的倍数输出Fizz,5的倍数输出Buzz.同时是3和5的倍数输出FizzBuzz,其他情况则打印原数字 for i in r ...