如何使用任意数量的单词获取单词列表的所有组合 [英] How to get ALL combinations of a list of words using ANY number of words

查看:90
本文介绍了如何使用任意数量的单词获取单词列表的所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了,但是找不到与我的查询匹配的任何内容.我见过很多解决方案,人们希望使用所有选项的数字/单词的所有组合,但没有一个像这样...

I have searched but I can't find anything that matches my query. I have seen lots of solutions where people want all combinations of numbers/words that use ALL the options, but none like this...

这是一个例子:

apple pear

这应该生成:

苹果

苹果梨
梨苹果

apple
pear
apple pear
pear apple

甚至...

apple pear banana

苹果

香蕉
苹果梨
苹果香蕉
梨香蕉
...
...
香蕉梨苹果

apple
pear
banana
apple pear
apple banana
pear banana
...
...
banana pear apple

关键是,所有可能以任何顺序使用零或一词的组合. :)

The key is, ALL possible combinations that use any of the words zero or one times in ANY order. :)

推荐答案

最后的答案

伪代码(未经测试)

$str = "apple pear banana";
$str_splode = explode(' ',$str);

echo showCombo($str_splode[0], $str_splode);

function showCombo($str, $arr){
    $ret = '';
    foreach($arr as $val){
       if($val != $str)
           $ret .= $str.showCombo($val, $arr);
    }
    return $ret;
}


运行代码: http://codepad.org/IUPJbhI7

<?php

$str = "apple pear banana orange";
$str_splode = explode(' ',$str);
print_r(showCombo(array(), $str_splode));

function showCombo($str_arr, $arr){
    $ret = array();
    foreach($arr as $val){
       if(!in_array($val, $str_arr)){
           $temp = $str_arr;
           $temp[] = $val;
           print_r($temp);
           $comb = showCombo($temp, $arr);
           if(count($comb) > 0)
              $ret[] = $comb;
       }
    }
    return $ret;
}
?>

这将返回所有个可能的组合

或者这看起来更好: http://codepad.org/KCLeRUYs

<?php

$str = "apple pear banana orange";
$str_splode = explode(' ',$str);
print_r(showCombo(array(), $str_splode));

function showCombo($str_arr, $arr){
    $ret = array();
    foreach($arr as $val){
       if(!in_array($val, $str_arr)){
           $temp = $str_arr;
           $temp[] = $val;
           $ret[$val] = $temp;
           $ret[$val][] = showCombo($temp, $arr);
       }
    }
    return $ret;
}

?>


或者如果您想查看平面键: http://codepad.org/95aNQzXB

此列表将它们全部列出: http://codepad.org/vndOI9Yj

And this one lists them all: http://codepad.org/vndOI9Yj

<?php

$str = "apple pear banana orange";
$str_splode = explode(' ',$str);
$combos = showCombo(array(), $str_splode);
foreach($combos as $key=>$array){
    echo $key.PHP_EOL;
    displayArrayByKey($key, $array);
}


function displayArrayByKey($str, $arr){
    foreach($arr as $key=>$array){
          $string = $str. " " . $key;
          echo $string . PHP_EOL; 
          if(count($array)> 0){
              displayArrayByKey($string, $array);
          }
    }
}

function showCombo($str_arr, $arr){
    $ret = array();
    foreach($arr as $val){
       if(!in_array($val, $str_arr)){
           $temp = $str_arr;
           $temp[] = $val;
           $ret[$val] = showCombo($temp, $arr);
       }
    }
    return $ret;
}

?>

这篇关于如何使用任意数量的单词获取单词列表的所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆