chapter_dynamic_programming/edit_distance_problem/ #600
Replies: 19 comments 20 replies
-
作者你好,我想问一下文中画图采用的是什么工具呀,感觉很精美。 |
Beta Was this translation helpful? Give feedback.
-
public static void main(String[] args)
{
String target = "hello";
String result = "a";
System.out.println(violent(target, result, target.length(), result.length()));
}
public static int violent(String target, String result, int ti, int ri)
{
if (ti < 1 || ri < 1)
{
return Math.max(ti,ri);
}
if (target.substring(ti - 1, ti)
.equals(result.substring(ri - 1, ri)))
{
return violent(target, result, ti - 1, ri - 1);
}
int delete = violent(target, result, ti - 1, ri) + 1;
int insert = violent(target, result, ti, ri - 1) + 1;
int replace = violent(target, result, ti - 1, ri - 1) + 1;
return Math.min(delete, Math.min(insert, replace));
} |
Beta Was this translation helpful? Give feedback.
-
大佬可以列一下相关的题单么,谢谢 |
Beta Was this translation helpful? Give feedback.
-
图14-30中,(i,j) 为(2,2)的时候,图为什么不加一呢?请大佬赐教 |
Beta Was this translation helpful? Give feedback.
-
请教一下K神,为什么我们要从字符串尾部开始考虑,是因为这样更便于思考?还是这样才可以将问题规模逐渐缩小呢? |
Beta Was this translation helpful? Give feedback.
-
感谢大作!图 14-29, 始终保持t不变,是否应该加上保持s不变的情况呢?状态转移结果是一致的。 |
Beta Was this translation helpful? Give feedback.
-
底部向上的思路会比较好想,遍历所有组合然后再找出最小的。从这个代码自然就会联想i,j会定位到一个最小编辑值;而i,j可以有3种情况可以抵达,这样就可以反向写出自上而下的递归进而转成动态放方程 private static void backtrackBottom(String word1, String word2, int i, int j, int currentDistance) {
//说明字符串 word1 已经处理完毕,那么剩余的字符是 word2.length() - j,这部分字符需要进行插入操作,所以更新 minDistance 时要加上这个插入的操作数。
if (i == word1.length()) {
minDistance = Math.min(minDistance, currentDistance + word2.length() - j);
return;
}
//说明字符串 word2 已经处理完毕,那么剩余的字符是 word1.length() - i,这部分字符需要进行删除操作,所以更新 minDistance 时要加上这个删除的操作数。
if (j == word2.length()) {
minDistance = Math.min(minDistance, currentDistance + word1.length() - i);
return;
}
if (word1.charAt(i) == word2.charAt(j)) {
backtrackBottom(word1, word2, i + 1, j + 1, currentDistance);
} else {
// Insert
backtrackBottom(word1, word2, i, j + 1, currentDistance + 1);
// Delete
backtrackBottom(word1, word2, i + 1, j, currentDistance + 1);
// Replace
backtrackBottom(word1, word2, i + 1, j + 1, currentDistance + 1);
}
} |
Beta Was this translation helpful? Give feedback.
-
想问大佬一件与文章无关的事,这么多种编程语言实现是怎么做到的,是有这方面的转换工具吗 |
Beta Was this translation helpful? Give feedback.
-
暴力搜索也可以,多谢大佬的思路 # 暴力搜索 编辑距离问题,每个字符只能插入、删除、替换一次
def edit_distance_fn(s1: str, s2: str, m: int, n: int) -> int:
m = len(s1) if m is None else m
n = len(s2) if n is None else n
# 边界判断
if m == 0:
return n
if n == 0:
return m
# 递归
if s1[m-1] == s2[n-1]:
return edit_distance_fn(s1, s2, m-1, n-1)
else:
return 1 + min(edit_distance_fn(s1, s2, m-1, n), edit_distance_fn(s1, s2, m, n-1), edit_distance_fn(s1, s2, m-1, n-1)) |
Beta Was this translation helpful? Give feedback.
-
hi,大佬。我遇到一个问题。 |
Beta Was this translation helpful? Give feedback.
-
hi! 请问 |
Beta Was this translation helpful? Give feedback.
-
大佬,没太明白空间优化的时候,为什么左上方的 |
Beta Was this translation helpful? Give feedback.
-
感觉状态的设计最难了,大佬们能分享一下心得吗 |
Beta Was this translation helpful? Give feedback.
-
图14-30 dp[i-1,j]和dp[i,j-1]的对应的颜色搞错了 |
Beta Was this translation helpful? Give feedback.
-
状态转移的表格很易懂, 就是它左上的三个格子的最小值, 问题是这个例子比前几个背包问题要抽象, 我不知道竖排bag和横排pack的每个节点里发生了什么, 不知道能不能做个图表示当前交换后变成了什么? 比如pag, ppg, ppp, 我就卡在这一步没法理解. |
Beta Was this translation helpful? Give feedback.
-
%%%%%%%%%%% |
Beta Was this translation helpful? Give feedback.
-
图 14-29 把这里的删除看成给
这样处理三种操作都会将最后一个字符变为相等,因此后面状态转移方程为这三种操作中的最小值加上这一步操作 |
Beta Was this translation helpful? Give feedback.
-
JS版本的暴力解法。 function minDistance(s, t) {
return dfs()
function dfs(i = 0, j = 0) {
/* 如果s的指针i先越界,则返回t的剩余字符串长度(即为剩余所需编辑次数) */
if (i === s.length) return t.length - j
/* 如果t的指针j先越界,则返回s的剩余字符串长度(即为剩余所需编辑次数) */
if (j === t.length) return s.length - i
/* 如果两指针所指字符相同(即本轮不为编辑距离的增加作贡献),则各向后移动1,递归执行 */
if (s[i] === t[j]) return dfs(i + 1, j + 1)
/* 需要编辑的一般情况: 本轮解 = 取三种可能操作结果之最小值 + 本轮操作1次 */
return Math.min(
dfs(i + 1, j), //删除s[i]后,剩余子问题为dp[i+1,j]
dfs(i, j + 1), //在s[i]后添加t[j]后,剩余子问题为dp[i,j+1]
dfs(i + 1, j + 1), //将s[i]替换为t[i],剩余子问题为dp[i+1,j+1]
) + 1
}
} |
Beta Was this translation helpful? Give feedback.
-
想出编辑距离问题的人真是个天才 |
Beta Was this translation helpful? Give feedback.
-
chapter_dynamic_programming/edit_distance_problem/
动画图解、一键运行的数据结构与算法教程
https://www.hello-algo.com/chapter_dynamic_programming/edit_distance_problem/
Beta Was this translation helpful? Give feedback.
All reactions