1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <?php $dict = array(); $string = array("bird", "drib", "ridb", "are", "aer", "rae", "rea"); for($i=0;$i<count($string);$i++) { insert_node($string[$i], &$dict); } find_relation("bird", &$dict);
function insert_node($word, $dict) { $index = word_id($word); for($node = $dict[$index]; $node != null; $node = $node['next']) { if (strcmp($node['word'], $word) == 0) { return; } } $p = array("word" => $word, "next" => $dict[$index]); $dict[$index] = $p; }
function word_id($word) { $index = 1; for($word_length = strlen($word), $i = 0; $i < $word_length; $i++) { $index = $index*(ord($word[$i])-ord('A')+1); } return $index % 300; }
function find_relation($current_word, $dict) { $temp = array(); $index = word_id($current_word); for($node = $dict[$index]; $node != null; $node = $node['next']) { if (strcmp($current_word, $node['word']) != 0) { $temp[] = $node['word']; } } echo "Find: ",$current_word," > Borther: ",implode(" ", $temp),"\n"; } ?>
|