改造一个字的最短路径到另一个

编程入门 行业动态 更新时间:2024-10-09 23:22:28
本文介绍了改造一个字的最短路径到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

对于数据结构的项目,我必须找到两个单词(之间的最短路径类似猫和狗),只改变一次一个字母。我们有一个拼字单词列表中找到我们的道路使用。例如:

For a Data Structures project, I must find the shortest path between two words (like "cat" and "dog"), changing only one letter at a time. We are given a Scrabble word list to use in finding our path. For example:

cat -> bat -> bet -> bot -> bog -> dog

我已经解决了使用广度优先搜索这个问题,但我追求更好的东西(我重新presented有线索的字典)。

I've solved the problem using a breadth first search, but am seeking something better (I represented the dictionary with a trie).

请给我一些想法,更有效的方法(在速度和内存方面)。可笑的东西和/或具有挑战性的是preferred。

Please give me some ideas for a more efficient method (in terms of speed and memory). Something ridiculous and/or challenging is preferred.

我问我的朋友(他是一名大三学生)之一,他说,是的没有的有效的解决了这个问题。他说我知道为什么,当我把算法过程。上任何意见?

I asked one of my friends (he's a junior) and he said that there is no efficient solution to this problem. He said I would learn why when I took the algorithms course. Any comments on that?

我们必须从字到词。我们不能猫 - > DAT - > DAG - >狗。我们还打印出遍历

We must move from word to word. We cannot go cat -> dat -> dag -> dog. We also have to print out the traversal.

推荐答案

新的答案

鉴于近期更新,你可以尝试用汉明距离作为启发式A *。它是因为它可容许的启发式不是要高估的距离

Given the recent update, you could try A* with the Hamming distance as a heuristic. It's an admissible heuristic since it's not going to overestimate the distance

OLD答案

您可以修改用于计算 Levenshtein距离的动态程序来获得操作顺序。

You can modify the dynamic-program used to compute the Levenshtein distance to obtain the sequence of operations.

编辑:如果有一个字符串常量数字,问题是在多项式时间。否则,它是NP难(这一切都没有在维基百科)..假设你的朋友都在谈论这个问题是NP难。

If there are a constant number of strings, the problem is solvable in polynomial time. Else, it's NP-hard (it's all there in wikipedia) .. assuming your friend is talking about the problem being NP-hard.

编辑:如果你的字符串长度相等,则可以使用汉明距离

If your strings are of equal length, you can use Hamming distance.

更多推荐

改造一个字的最短路径到另一个

本文发布于:2023-11-05 02:47:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559699.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:一个字   最短   路径

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!