PHP字典类?还是替代? [英] PHP dictionary class? or alternative?

查看:103
本文介绍了PHP字典类?还是替代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在寻找的是在PHP中实现字典的某种类或方法。
例如,如果我正在构建一个单词unscrambler - 让我说我使用字母'a,e,l,p,p'。安排的可能性很大 - 如何只显示那些是真实的话(苹果,苍白等)?

Basically, what I'm looking for is some kind of class or method to implement a dictionary in PHP. For example, if I was building a word unscrambler - lets say I used the letters 'a,e,l,p,p'. The number of possibilities for arrangement is huge - how do I only display those which are actual words (apple, pale etc )?

谢谢!

推荐答案

经典的单词查找问题可以使用 Trie

Classically word lookup problems can be efficiently solved using a Trie.

我建议从 WordNet ,将其存储在Trie中,然后执行快速查找可能的单词。

I would suggest finding a word list, say, from WordNet, store it in a Trie, and then perform fast lookups of possible words.

解决方案的形式如下:


  1. 加载单词列表

  2. 将单词列表存储在一个trie中

  3. 接受单词的输入来解密

  4. 尝试排列i = 1 .. N

  1. load the word list
  2. store the word list in a trie
  3. accept input for a word to unscramble
  4. try permutations i=1..N

a。查找排列我使用trie

a. lookup permutation i using the trie

b。如果有积极的结果,请存储以进行显示

b. if there's a positive result, store this for display

c。迭代(i ++)

c. iterate (i++)

从3重复。

编辑:

这里的一个附注是,对于任何N个长度的字符字,可能有N!需要查找(7个字符,为5040)。您应该考虑对特里查询算法进行一些优化。例如,您可以提前排除无效的子串,而不是重复结束排列,从而大大提高效率。

A side note here is that for any N length character word there could be N! required lookups (for 7 characters that would be 5040). You should consider making some optimizations to the trie lookup algorithm. For instance, you gain substantial efficiency by ruling out invalid substrings early, and not repeating end permutations.

给出苹果这个词,如果你选择了ppl作为前三个字符的排列,则不会找到任何单词。所以,无论你如何排列a和e,最后你不能构造一个单词。提前终止排列可能对您的算法的效率很重要。

e.g. given the word apple, if you had the permutation where you selected "ppl" as the first three characters, no word will be found. So, no matter how you permute the a and the e at the end you cannot construct a word. Early termination of permutations may be important to your algorithm's efficiency.

这篇关于PHP字典类?还是替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆