可变长度的独特排列

时间:2014-05-16 14:37:38

标签: php algorithm

我需要获得任意长度的一系列独特排列:

值数组: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);

1 个答案:

答案 0 :(得分:0)

我对它采取了一个裂缝:

function get_perms(chars) {
    var perms = [ chars[0] ];
    for (var i = 1; i < chars.length; i += 1) {
        len = perms.length;
        for (var j = 0; j < len; j += 1) {
            perms.push(perms[j] + chars[i]);
        }
    }
    return perms;
}

var chars = [ "a", "b", "c", "d", "e" ], perms = [];
for (var i = 0; i < chars.length; i += 1) {
    perms = perms.concat(get_perms(chars.slice(i)));
}
console.log(perms);

大纲是:

从完整集[ "a", "b", "c", "d", "e", .. ]

开始

生成所有提升排列,aabacabc等,但绝不会acb。我这样做是从["a"]开始,现在将"b"添加到所有这些并合并以获取:["a", "ab"],现在将"c"添加到所有这些并合并:{{1等等。

为集["a", "ab", "ac", "abc"]再为[ "b", "c", "d", "e", .. ]等再次执行此操作。

这应该为您提供所有排列而不重复。


示例输出([ "c", "d", "e", .. ]):

[ "a", "b", "c", "d", "e" ]

在我的机器上生成一整套13个字符所需的时间不到1秒,因此我不会费心去测量。另外,我意识到我做的是JavaScript而不是PHP,它不应该太难转换。