用java中的乱序字母组成一个单词
我正在做一个游戏项目,其中我们必须使用给定的字母集动态地形成单词...给定的字母集也可能包含重复项..在形成单词时,我们可以使用给定字母集中的字母任意次数(比如两次或三次)...帮助我用一种算法从给定的集合中形成所有可能的有意义的单词
谢谢大家
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种简单的方法是创建每种可能的字母顺序,然后将它们中的每一个与您的字典进行比较。
您可以通过将字典存储在有助于快速查找的数据结构中来对其进行一些改进。 (哈希表、树等)我一直想实现一个 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.
许多个月前,我为填字游戏解决器做了类似的事情。我基本上拿了一个字典文件并对其进行了修改,使其看起来像:
然后,对于给定的一组字母,我可以对它们进行排序并使用类似的东西:
这会给我候选单词。
如果您正在寻找可以使用少于完整集合的单词,则必须围绕它包装一些排列/组合代码,但给出的算法非常有效。
对于Java,我会考虑在内存中维护一个哈希表(假设你有空间)或使用外部数据库,其中查找键是排序的变量,当然允许重复,因为
pore
和rope
都来自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:
Then, for a given set of letters, I could just sort them and use something like:
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
andrope
would both come fromeorp
.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.