【发布时间】:2014-07-05 01:39:16
【问题描述】:
我需要得到一个任意长度的唯一排列数组:
值数组:a、b、c、d、e、f、g、h、i、j、k、l、m(共 13 个)
预期结果:a、b、c、ab、ac、bc、abc、....
ab == ba(重复)
abc == acb == bca == bac == ...(重复)
到目前为止,我对它进行了蛮力攻击,但对于 13 个元素,这有点乐观。我需要一些更聪明的东西,不幸的是超出了我的数学能力。
我目前的疯狂解决方案:
// Returns the total number of $count-length strings generatable from $letters.
function getPermCount($letters, $count) {
$result = 1;
// k characters from a set of n has n!/(n-k)! possible combinations
for($i = strlen($letters) - $count + 1; $i <= strlen($letters); $i++) {
$result *= $i;
}
return $result;
}
// Decodes $index to a $count-length string from $letters, no repeat chars.
function getPerm($letters, $count, $index) {
$result = array();
for($i = 0; $i < $count; $i++) {
$pos = $index % strlen($letters);
$result[] = $letters[$pos];
$index = ($index-$pos)/strlen($letters);
$letters = substr($letters, 0, $pos) . substr($letters, $pos+1);
}
sort($result); // to be able and remove dupes later
return implode("", $result);
}
$r = array();
$letters = 'abcdefghijklm';
$len = strlen($letters);
$b = 0;
for ($c = 1; $c <= $len; $c++) {
$p = getPermCount($letters, $c);
echo $c." {".$p." perms} - ";
for($i = 0; $i < $p; $i++) {
$r[] = getPerm($letters, $c, $i)." ";
if ($b > 4000000) {
$r = array_flip($r); // <= faster alternative to array_unique
$r = array_flip($r); // flipping values to keys automaticaly removes dupes
$b = 0;
} else {
$b++;
}
}
$r = array_flip($r); // <= faster alternative to array_unique
$r = array_flip($r); // flipping values to keys automaticaly removes dupes
echo count($r)." unique\n";
}
print_r($r);
【问题讨论】: