第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP
看起来比较棘手的一道题(列DP方程还是要大胆猜想。。)
DP方程该怎么列呢?
dp[i][j]表示字符串a[0....i-1]转化为b[0....j-1]的最少距离
转移方程分三种情况考虑 分别对应三中操作
因为只需要三个值就可以更新dp[i][j] 我们可以把空间复杂度降低到O(n)
- Replace
word1[i - 1]
byword2[j - 1]
(dp[i][j] = dp[i - 1][j - 1] + 1 (for replacement)
); - Delete
word1[i - 1]
andword1[0..i - 2] = word2[0..j - 1]
(dp[i][j] = dp[i - 1][j] + 1 (for deletion)
); - Insert
word2[j - 1]
toword1[0..i - 1]
andword1[0..i - 1] + word2[j - 1] = word2[0..j - 1]
(dp[i][j] = dp[i][j - 1] + 1 (for insertion)
).class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.length(), n = word2.length();
vector<int> cur(m + 1, 0);
for (int i = 1; i <= m; i++)
cur[i] = i;
for (int j = 1; j <= n; j++) {
int pre = cur[0];
cur[0] = j;
for (int i = 1; i <= m; i++) {
int temp = cur[i];
if (word1[i - 1] == word2[j - 1])
cur[i] = pre;
else cur[i] = min(pre + 1, min(cur[i] + 1, cur[i - 1] + 1));
pre = temp;
}
}
return cur[m];
}
};
第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP的更多相关文章
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- LeetCode - 72. Edit Distance
最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...
- [leetcode]72. Edit Distance 最少编辑步数
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- [leetcode] 72. Edit Distance (hard)
原题 dp 利用二维数组dp[i][j]存储状态: 从字符串A的0~i位子字符串 到 字符串B的0~j位子字符串,最少需要几步.(每一次删增改都算1步) 所以可得边界状态dp[i][0]=i,dp[0 ...
- 【Leetcode】72 Edit Distance
72. Edit Distance Given two words word1 and word2, find the minimum number of steps required to conv ...
- 刷题72. Edit Distance
一.题目说明 题目72. Edit Distance,计算将word1转换为word2最少需要的操作.操作包含:插入一个字符,删除一个字符,替换一个字符.本题难度为Hard! 二.我的解答 这个题目一 ...
- 达拉草201771010105《面向对象程序设计(java)》第十八周学习总结
达拉草201771010105<面向对象程序设计(java)>第十八周学习总结 实验十八 总复习 实验时间 2018-12-30 1.实验目的与要求 (1) 综合掌握java基本程序结构 ...
随机推荐
- [HDU2896]病毒侵袭(AC自动机)
传送门 题目中文描述,赞! 除了val记录id以外就是模板. 注意:每次数组都要清0.0 ——代码 #include <cstdio> #include <queue> #in ...
- 2016 Multi-University Training Contest 5 solutions BY ZSTU
ATM Mechine E(i,j):存款的范围是[0,i],还可以被警告j次的期望值. E(i,j) = \(max_{k=1}^{i}{\frac{i-k+1}{i+1} * E(i-k,j)+\ ...
- 《TCP/IP详解卷1:协议》——第1章:概述(转载)
1.引言 很多不同的厂家生产各种型号的计算机,它们运行完全不同的操作系统,但TCP/IP协议族允许它们互相进行通信.TCP/IP起源于60年代末美国政府资助的一个分组交换网络研究项目,到90年代已发展 ...
- msp430项目编程22
msp430中项目---充电控制系统 1.定时器工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习
- for-else和wihle-else组合用法
当for和else组合一起使用的时候,for循环正常执行完毕,会执行else语句,否则,不会执行else语句 for i in range(10): if i == 2: break else: pr ...
- POJ 2348 Euclid's Game【博弈】
题目链接: http://poj.org/problem?id=2348 题意: 给定两个数,两个人每次从较大数中减去较小数的倍数,谁先得到0谁获胜,为谁赢? 分析: 令一种可能出现的整数对为(a,b ...
- D. Little Artem and Dance---cf669D
http://codeforces.com/problemset/problem/669/D 题目大意: 有n对人 男生和女生 开始时 每个人的标号是(1,2,3,...n) 女生们围成一个圈 男 ...
- 微服务 Framework
Dubbo :https://github.com/dubbo Spring Cloud :https://github.com/spring-cloud
- Python pandas学习笔记
参考文献:<Python金融大数据分析> #导入模块 import pandas as pd #生成dataframe df = pd.DataFrame([10,20,30,40], c ...
- VS2012关于hash_map的使用简略
VS关于hash_map使用的一些经常使用构造方法汇总,包含基本类型和结构体,相信够一般模仿使用: # include<hash_map> #include<iostream> ...