用java中的乱序字母组成一个单词

发布于 2024-10-19 12:15:43 字数 137 浏览 3 评论 0原文

我正在做一个游戏项目,其中我们必须使用给定的字母集动态地形成单词...给定的字母集也可能包含重复项..在形成单词时,我们可以使用给定字母集中的字母任意次数(比如两次或三次)...帮助我用一种算法从给定的集合中形成所有可能的有意义的单词

谢谢大家

i'm doing a game project,in which we have to form words dynamically with the given set of letters... the given set of letters may contain duplicate also.. while forming words we can use a letter from a given set of letters for any number of times(say for twice or thrice)... help me with an algorithm to form all possible meaningful words from the given set

Thank u all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独岁月 2024-10-26 12:15:43

一种简单的方法是创建每种可能的字母顺序,然后将它们中的每一个与您的字典进行比较。

您可以通过将字典存储在有助于快速查找的数据结构中来对其进行一些改进。 (哈希表、树等)我一直想实现一个 28 元树来快速访问字典单词,但还没有抽出时间来实现。

The simple approach is to create every possible ordering of letters, then compare each one of them to your dictionary.

You can refine it a bit by storing the dictionary in a data structure which facilitates quick lookups. (hash table, tree, etc) I've been meaning to implement a 28-ary tree for quick dictionary word access, but haven't got around to it yet.

那请放手 2024-10-26 12:15:43

许多个月前,我为填字游戏解决器做了类似的事情。我基本上拿了一个字典文件并对其进行了修改,使其看起来像:

aardvark:aaadkrr
albatross:aablorsst

然后,对于给定的一组字母,我可以对它们进行排序并使用类似的东西:

grep ':{sorted letters}

这会给我候选单词。

如果您正在寻找可以使用少于完整集合的单词,则必须围绕它包装一些排列/组合代码,但给出的算法非常有效。

对于Java,我会考虑在内存中维护一个哈希表(假设你有空间)或使用外部数据库,其中查找键是排序的变量,当然允许重复,因为 porerope 都来自 eorp

虽然我的基于 grep 的解决方案非常适合我自己的目的,但您可能不想在强大的应用程序中依赖外部工具和子流程。

mywords.txt | sed 's/:.*$//'

这会给我候选单词。

如果您正在寻找可以使用少于完整集合的单词,则必须围绕它包装一些排列/组合代码,但给出的算法非常有效。

对于Java,我会考虑在内存中维护一个哈希表(假设你有空间)或使用外部数据库,其中查找键是排序的变量,当然允许重复,因为 porerope 都来自 eorp

虽然我的基于 grep 的解决方案非常适合我自己的目的,但您可能不想在强大的应用程序中依赖外部工具和子流程。

I did something similar for a crossword solver many moons ago. I basically took a dictionary file and modified it so it looked like:

aardvark:aaadkrr
albatross:aablorsst

Then, for a given set of letters, I could just sort them and use something like:

grep ':{sorted letters}

and that would give me the candidate words.

You'll have to wrap some permutation/combination code around that if you're looking for words that can use less than the full set, but the algorithm given was very efficient.

For Java, I'd consider either maintaining a hash table in-memory (assuming you have space) or using an external database where the lookup keys are the sorted varaiations, allowing duplicates of course, since pore and rope would both come from eorp.

While my grep-based solution was fine for my own purposes, you probably don't want to rely on external tools and sub-processes in a robust application.

mywords.txt | sed 's/:.*$//'

and that would give me the candidate words.

You'll have to wrap some permutation/combination code around that if you're looking for words that can use less than the full set, but the algorithm given was very efficient.

For Java, I'd consider either maintaining a hash table in-memory (assuming you have space) or using an external database where the lookup keys are the sorted varaiations, allowing duplicates of course, since pore and rope would both come from eorp.

While my grep-based solution was fine for my own purposes, you probably don't want to rely on external tools and sub-processes in a robust application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文