php递归获取所有可能的字符串

标签 php recursion

这是我获取所有可能性的代码:

$seq[1] = 'd';
$seq[2] = 'f';
$seq[3] = 'w';
$seq[4] = 's';

for($i = 1; $i < 5; $i++)
{
    $s['length_1'][] = $seq[$i];
    $c1++;

    for($i2 = $i+1; $i2 < 5; $i2++)
    {
        $s['length_2'][] = $seq[$i].$seq[$i2]; 
        $last = $seq[$i].$seq[$i2]; 
        $c2++;

        for($i3 = $i2+1; $i3 < 5; $i3++)
        { 
            $s['length_3'][] = $last.$seq[$i3];
            $last = $last.$seq[$i3];    
            $c3++;

            for($i4 = $i3+1; $i4 < 5; $i4++)
            {
                $s['length_4'][] = $last.$seq[$i4];   
                $c4++;  
            }
        }
    }
}

for($i = 0; $i < $c1; $i++)
    echo $s['length_1'][$i].'<br>'; 

for($i = 0; $i < $c2; $i++)
    echo $s['length_2'][$i].'<br>';   

for($i = 0; $i < $c3; $i++)
    echo $s['length_3'][$i].'<br>';  

for($i = 0; $i < $c4; $i++)
    echo $s['length_4'][$i].'<br>';    

但是如果我想添加更多,那么我将不得不再添加一个循环。那么,我该如何使用递归呢?我努力,我努力,但我真的做不到。 请帮助并尽可能简单地发布示例。

谢谢。

最佳答案

这里有一个算法,

function getCombinations($base,$n){

$baselen = count($base);
if($baselen == 0){
    return;
}
    if($n == 1){
        $return = array();
        foreach($base as $b){
            $return[] = array($b);
        }
        return $return;
    }else{
        //get one level lower combinations
        $oneLevelLower = getCombinations($base,$n-1);

        //for every one level lower combinations add one element to them that the last element of a combination is preceeded by the element which follows it in base array if there is none, does not add
        $newCombs = array();

        foreach($oneLevelLower as $oll){

            $lastEl = $oll[$n-2];
            $found = false;
            foreach($base as  $key => $b){
                if($b == $lastEl){
                    $found = true;
                    continue;
                    //last element found

                }
                if($found == true){
                        //add to combinations with last element
                        if($key < $baselen){

                            $tmp = $oll;
                            $newCombination = array_slice($tmp,0);
                            $newCombination[]=$b;
                            $newCombs[] = array_slice($newCombination,0);
                        }

                }
            }

        }

    }

    return $newCombs;


}

我知道它在任何方面都不是有效的,但在小范围内使用应该不是问题

第一个基本参数是一个数组,其中包含生成组合时要考虑的元素。

为了简单的使用和输出:

var_dump(getCombinations(array("a","b","c","d"),2));

输出是

array
  0 => 
    array
      0 => string 'a' (length=1)
      1 => string 'b' (length=1)
  1 => 
    array
      0 => string 'a' (length=1)
      1 => string 'c' (length=1)
  2 => 
    array
      0 => string 'a' (length=1)
      1 => string 'd' (length=1)
  3 => 
    array
      0 => string 'b' (length=1)
      1 => string 'c' (length=1)
  4 => 
    array
      0 => string 'b' (length=1)
      1 => string 'd' (length=1)
  5 => 
    array
      0 => string 'c' (length=1)
      1 => string 'd' (length=1)

要列出一个数组的所有子集,使用这个组合算法只需执行

$base =array("a","b","c","d");

for($i = 1; $i<=4 ;$i++){
    $comb = getCombinations($base,$i);

    foreach($comb as $c){
        echo implode(",",$c)."<br />";
    }

}

输出是

a
b
c
d
a,b
a,c
a,d
b,c
b,d
c,d
a,b,c
a,b,d
a,c,d
b,c,d
a,b,c,d

关于php递归获取所有可能的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4279722/

相关文章:

php - "Could not execute Model_Hotel::__construct()"与 mysql_fetch_object,属性保持未初始化

c# - 这个递归函数如何返回 true?

python - 如何删除未知深度的深层嵌套字典中的空字段或无字段?

php - 如何检查 PHP 数组是关联数组还是顺序数组?

php - 高效有效的表单验证

php - 无法在网页上显示数据库中的图像

recursion - 如何实现惰性 "reducing map"函数?

java - Java中递归查找二叉树中的最小值

c++ - 递归迭代而不重新绑定(bind)引用

php - 将 HTML 表格另存为图像