これは、必要な再帰関数です。アイデアは、長さと文字が与えられると、その文字を含まない 1 文字短いすべてのシーケンスを最初に生成することです。最後に新しい文字を追加すると、その文字を含むシーケンスの最初の部分が得られます。次に、新しい文字を左に移動します。右側の新しい文字を含む文字の各シーケンスを循環します。
したがって、gen(5, d) があれば、次のように始まります。
(aaaa)d
(aaab)d
...
(cccc)d
それがACの組み合わせで終わったら、それはするでしょう
(aaa)d(a)
...
(aaa)d(d)
(aab)d(d)
...
(ccc)d(d)
次に、d を 4 番目の文字として使用すると、3 番目の文字に移動します。
(aa)d(aa)
などなど
<?php
/**
* Word Combinations (version c) 6/22/2009 1:20:14 PM
*
* Based on pseudocode in answer provided by Erika:
* http://stackoverflow.com/questions/1024471/generating-ordered-weighted-combinations-of-arbitrary-length-in-php/1028356#1028356
* (direct link to Erika's answer)
*
* To see the results of this script, run it:
* http://stage.dustinfineout.com/stackoverflow/20090622/word_combinations_c.php
**/
init_generator();
function init_generator() {
global $words;
$words = array('a','b','c');
generate_all(5);
}
function generate_all($len){
global $words;
for($i = 0; $i < count($words); $i++){
$res = generate($len, $i);
echo join("<br />", $res);
echo("<br/>");
}
}
function generate($len, $max_index = -1){
global $words;
// WHEN max_index IS NEGATIVE, STARTING POSITION
if ($max_index < 0) {
$max_index = count($words) - 1;
}
$list = array();
if ($len <= 0) {
$list[] = "";
return $list;
}
if ($len == 1) {
if ($max_index >= 1) {
$add = generate(1, ($max_index - 1));
foreach ($add as $addit) {
$list[] = $addit;
}
}
$list[] = $words[$max_index];
return $list;
}
if($max_index == 0) {
$list[] = str_repeat($words[$max_index], $len);
return $list;
}
for ($i = 1; $i <= $len; $i++){
$prefixes = generate(($len - $i), ($max_index - 1));
$postfixes = generate(($i - 1), $max_index);
foreach ($prefixes as $pre){
//print "prefix = $pre<br/>";
foreach ($postfixes as $post){
//print "postfix = $post<br/>";
$list[] = ($pre . $words[$max_index] . $post);
}
}
}
return $list;
}
?>