0

我有一个小的个人项目,我正在努力完成。我需要获取一串字符并尝试从所述字符串的变体中“创建”单词;检查带有已知单词列表的文本文件(单词由新行分隔)。

总之:

  • 用户提供字符串 $chars_provided(即“jdlwhfushfmgh”),
  • 然后分解 $chars_provided
  • 爆炸的 $chars_provided 是随机排列的,以尝试从所述字符串创建单词
  • 根据字典文本文件创建了检查/验证的单词以确保它们存在
  • 结果以创建单词的字符数显示,限制为 100 个单词。

我脑子里有这个概念,只是不知道应该怎么做,我只是在寻找可以向我解释这个过程的人。

<?php

// list of words, one per line
$dictionary = file_get_contents('dictionary.txt');

// provided characters, in the end by user
$chars_provided = "a,t,w,q,u,i,f,d,s,b,v,x,o";

// count the total # of characters
$chars_count = strlen($chars_provided);

// display given information so far
echo "The letters '$chars_provided' were entered, totaling $chars_count letters.";

// explode the characters by using the comma designator
$break_chars = explode(",", $chars_provided);

foreach ($break_chars as $letter) {
    echo "$letter[0]";
}
4

2 回答 2

2

如果您获取字典中每个单词的字母计数,保留它,然后匹配用户输入的字符计数,这会更容易。

例如,对于“aaab”,任何少于(或等于)3 个“a”、少于(或等于)1 个“b”且没有其他字符的单词都将匹配。

//// 1. Grab letter counts for your user input.

$user_input_chars = 'abcdefg'; // for example
$user_in_letter_counts = get_letter_counts($user_input_chars);

// $letters[$char][$num] will contain all words that have exactly $num number of $char characters
$letters = array('a' => array(), 'b' => array(), /* ...,*/ 'z' => array());

//// 2. Generate list of words with at least $number_of quantity of $letter characters
//     (only have to be done once for any amount of user input if you keep this in memory)
foreach ($words as $word){
    // get letter counts for each type of character for this word
    $letter_counts = get_letter_counts($word);
    // store in array of letters and count
    foreach($letter_counts as $letter => $number_of){
        // we already have a word that had $number_of $letter characters; add word to existing array
        if (isset($letters[$letter][$number_of])){
            $letters[$letter][$number_of][] = $word;
        } // make array to record that this word has $number_of $letter characters
        else {
            $letters[$letter][$number_of] = array($word);
        }
        $number_of--;
    }
}

//// 3. Find matching words.
$potential_words = array();
foreach ($letters as $letter => $arr){
    foreach($arr as $num => $words){
        // if this array has less than or equal to the number of $letter characters that the user input has,
        // add the words to the potential match list for that character
        if ($num <= $arr[$user_in_letter_counts[$letter]]){
            $potential_words[$letter] = array_merge($potential_words[$letter], $words);
        }
    }
}

// the words must have met the requirement for each character, so only grab words that satisfy all conditions
$all_matching_words = array_intersect($potential_words['a'], $potential_words['b'], /* ..., */ $potential_words['z']);

// (It should be trivial to just grab 100 of these.)

function get_letter_counts($word){
    $result = array();
    $result['a'] = substr_count($my_word, 'a');
    $result['b'] = substr_count($my_word, 'b');
    // ...
    $result['z'] = substr_count($my_word, 'z');
    return $result;
}
于 2012-11-02T23:27:14.267 回答
0

Hope you can use this.

$file = file_get_contents("dictionary.txt");
    $SearchString = "jdlwhfushfmgh/maybeasencondword";
    $breakstrings = explode('/',$SearchString);

    foreach ($breakstrings as $values)
    {
        if(!strpos($file, $values))
        {
            echo $values." string not found!\n";
        }
        else
        {
            echo $values." string Found!\n";
        }
于 2012-11-02T20:42:33.180 回答