1

假设我们有一个包含单词的字符串:
1 2 3 4 5

我想要三个单词的所有组合,但顺序不能改变。字符串的长度可以从 4 个单词到 7 个单词不等。不再是因为会有太多的组合。因此示例 1 2 3 4 5 的结果将是一个带有字符串的数组:
1 2 3
1 2 4
1 2 5
1 3 4
1 3 5
1 4 5
2 3 4
2 3 5
2 4 5
3 4 5

我一直在考虑这个问题。一个想法是把它变成一个数组,为单词的数量生成一个二进制代码,如果它是 1,那么它会显示这个词,否则它不会(所以你会得到从 11100 到 00111 的任何东西)。但这可能不是最优的。

在这种情况下,速度很重要,因为可能必须在几分钟内完成数万次。

4

2 回答 2

1

我使用了 Vicky 的答案并做了一些修改。我认为这会如你所愿

function everyCombination($array) {
    $tempcount = 3;                            //lenght of the combinations you want
    $arrayCount      = count($array);
    $maxCombinations = pow($arrayCount, $arrayCount);
    $returnArray     = array();
    $conversionArray = array();
    foreach ($array as $key => $value) {
        $conversionArray[base_convert($key, 10, $arrayCount)] = $value;
    }
    for ($i = 0; $i < $maxCombinations; $i++) {
        $flag = 1;
        $combination = base_convert($i, 10, $arrayCount);
        $combination = str_pad($combination, $tempcount, "0", STR_PAD_LEFT);
        $temp = strtr($combination, $conversionArray);
        for($j = 0; $j < $tempcount-1; $j++){

            if($temp[$j] >= $temp[$j+1] || $flag == 0)
                $flag = 0;
            else
                $flag = 1;
        }
        if($flag && strlen($temp) == $tempcount)
            $returnArray[] = $temp;
    }
    return $returnArray;
}

print_r(everyCombination(array(1,2,3,4)));
于 2013-11-08T06:07:00.663 回答
0

你可以这样做:

<?php
function everyCombination($array)
{
    $arrayCount = count($array);
    $resultArray = array();
    $veryFinal = array();
    $maxCombinations = pow($arrayCount, $arrayCount);
    $returnArray = array();
    $conversionArray = array();
    foreach ($array as $key => $value) {
        $conversionArray[base_convert($key, 10, $arrayCount)] = $value;
    }
    for ($i = 0; $i < $maxCombinations; $i++) {
        $combination = base_convert($i, 10, $arrayCount);
        $combination = str_pad($combination, $arrayCount, "0", STR_PAD_LEFT);
        $returnArray[] = strtr($combination, $conversionArray);
    }
    $finalArray = array();
    foreach ($returnArray as $sepArray) {
        array_push($finalArray, array_unique(str_split($sepArray)));
    }
    foreach ($finalArray as $result) {
        $temp = implode(",", $result);
        if (count($result) == 3) {
            array_push($resultArray, $temp);
        }
    }
    $tempFinal = array_unique($resultArray);
    foreach ($tempFinal as $final) {
        array_push($veryFinal, $final);
    }
    return $veryFinal;
}


$res1 = (everyCombination(array(1, 2, 3, 4)));
$res2 = (everyCombination(array('a','b','c','d')));
var_dump($res1);
var_dump($res2);

它给:

array (size=24)
  0 => string '1,2,3' (length=5)
  1 => string '1,2,4' (length=5)
  2 => string '1,3,2' (length=5)
  3 => string '1,3,4' (length=5)
  4 => string '1,4,2' (length=5)
  5 => string '1,4,3' (length=5)
  6 => string '2,1,3' (length=5)
  7 => string '2,1,4' (length=5)
  8 => string '2,3,1' (length=5)
  9 => string '2,3,4' (length=5)
  10 => string '2,4,1' (length=5)
  11 => string '2,4,3' (length=5)
  12 => string '3,1,2' (length=5)
  13 => string '3,1,4' (length=5)
  14 => string '3,2,1' (length=5)
  15 => string '3,2,4' (length=5)
  16 => string '3,4,1' (length=5)
  17 => string '3,4,2' (length=5)
  18 => string '4,1,2' (length=5)
  19 => string '4,1,3' (length=5)
  20 => string '4,2,1' (length=5)
  21 => string '4,2,3' (length=5)
  22 => string '4,3,1' (length=5)
  23 => string '4,3,2' (length=5)
array (size=24)
  0 => string 'a,b,c' (length=5)
  1 => string 'a,b,d' (length=5)
  2 => string 'a,c,b' (length=5)
  3 => string 'a,c,d' (length=5)
  4 => string 'a,d,b' (length=5)
  5 => string 'a,d,c' (length=5)
  6 => string 'b,a,c' (length=5)
  7 => string 'b,a,d' (length=5)
  8 => string 'b,c,a' (length=5)
  9 => string 'b,c,d' (length=5)
  10 => string 'b,d,a' (length=5)
  11 => string 'b,d,c' (length=5)
  12 => string 'c,a,b' (length=5)
  13 => string 'c,a,d' (length=5)
  14 => string 'c,b,a' (length=5)
  15 => string 'c,b,d' (length=5)
  16 => string 'c,d,a' (length=5)
  17 => string 'c,d,b' (length=5)
  18 => string 'd,a,b' (length=5)
  19 => string 'd,a,c' (length=5)
  20 => string 'd,b,a' (length=5)
  21 => string 'd,b,c' (length=5)
  22 => string 'd,c,a' (length=5)
  23 => string 'd,c,b' (length=5)
于 2013-11-08T05:43:55.633 回答