将scrambled words与未加扰的wordlist php进行比较

时间:2009-12-20 18:12:54

标签: php

我搜索一种方法来比较加扰的单词和一个充满未加扰单词的单词表,例如:

扰乱的词是“lonbayb”,词汇表中的某个地方是“babylon”。脚本应该显示未解读的单词

任何想法如何解决这个问题?

2 个答案:

答案 0 :(得分:6)

一个简单的解决方案是在比较之前按字母顺序对加扰和未加扰的单词中的字母进行排序。我称之为“改组”:

"babylon" ==> "abblnoy"

实际上,你应该从你的参考词表中创建第二个词表,参考词表的条目就像这样洗牌。

然后当你看到一个新单词并想知道它是否在列表中时,以同样的方式进行随机播放,你可以在你的混乱参考列表中进行简单的搜索。如果按字母顺序对随机引用列表中的单词进行排序,您甚至可以对其进行二进制搜索。或者你将混乱的参考词放入一个哈希集或一个b树中...无论什么都很容易快速搜索。

答案 1 :(得分:1)

使用str_shuffle()来洗牌。 要将混洗后的字符串与单词表进行比较,您可以使用count_chars()

class WordFinder
{
    protected $_wordList;
    protected $_map;

    public function __construct(array $wordList)
    {
        $this->_wordList = $wordList;
    }

    protected function _initMap()
    {
        if(!is_array($this->_map)) {
            $this->_map = array();
            foreach($this->_wordList as $word) {
                $key = count_chars($word, 3);
                if(!isset($this->_map[$key])) {
                    $this->_map[$key] = array();
                }
                $this->_map[$key][] = $word;
            }
        }
    }

    public function findWords($searchWord)
    {
        $searchWord = count_chars($searchWord, 3);
        $this->_initMap();
        if(isset($this->_map[$searchWord])) {
            return $this->_map[$searchWord];
        }
        return false;
    }    
}

然后做

$list   = array('evil', 'live', 'vile', 'cat');
$finder = new WordFinder($list);
var_dump($finder->findWords('evli'));

这将返回

array(3) {
  [0]=>
  string(4) "evil"
  [1]=>
  string(4) "live"
  [2]=>
  string(4) "vile"
}

修改 我已经用这个版本交换了原始代码,因为它使用大型单词列表更好地执行很多。我已经在我的2,2 Ghz双核心上测试了上面的内容,它将在仅仅0.08秒的10000个单词的集合中完成10000次调用findWords()。另一个版本需要207秒。请参阅旧版本的修订版。