Home > Backend Development > PHP Tutorial > 组词算法的思路

组词算法的思路

WBOY
Release: 2016-06-13 13:26:24
Original
1428 people have browsed it

求一个组词算法的思路
有一个组词的需求,请大家帮忙看下,要求如下:

给定一组词,每行一个,如:

裤子
牛仔
纯棉
低腰
加肥

用上面的词组成下面的格式

裤子
牛仔
纯棉
低腰
加肥
裤子牛仔
裤子纯棉
裤子低腰
裤子加肥
裤子牛仔纯棉
裤子牛仔低腰
裤子牛仔加肥
裤子牛仔纯棉低腰
裤子牛仔纯棉加肥
裤子牛仔纯棉低腰加肥
牛仔纯棉
牛仔低腰
牛仔加肥
牛仔纯棉低腰
........

这样依次类推一层一层的组词。

求一个这个需求的算法思路。

------解决方案--------------------
很简单的一个组合问题, 每个词语可选/可不选, 递归跑一下就行了.
------解决方案--------------------
排列组合,是你要的结果么?

PHP code
$arr=array('裤子','牛仔','纯棉','低腰','加肥');
for($i=1,$count=count($arr);$i Array
        (
            [0] => 裤子
            [1] => 牛仔
            [2] => 纯棉
            [3] => 低腰
            [4] => 加肥
        )

    [2] => Array
        (
            [0] => 裤子牛仔
            [1] => 裤子纯棉
            [2] => 裤子低腰
            [3] => 裤子加肥
            [4] => 牛仔纯棉
            [5] => 牛仔低腰
            [6] => 牛仔加肥
            [7] => 纯棉低腰
            [8] => 纯棉加肥
            [9] => 低腰加肥
        )

    [3] => Array
        (
            [0] => 裤子牛仔纯棉
            [1] => 裤子牛仔低腰
            [2] => 裤子牛仔加肥
            [3] => 裤子纯棉低腰
            [4] => 裤子纯棉加肥
            [5] => 裤子低腰加肥
            [6] => 牛仔纯棉低腰
            [7] => 牛仔纯棉加肥
            [8] => 牛仔低腰加肥
            [9] => 纯棉低腰加肥
        )

    [4] => Array
        (
            [0] => 裤子牛仔纯棉低腰
            [1] => 裤子牛仔纯棉加肥
            [2] => 裤子牛仔低腰加肥
            [3] => 裤子纯棉低腰加肥
            [4] => 牛仔纯棉低腰加肥
        )

    [5] => Array
        (
            [0] => 裤子牛仔纯棉低腰加肥
        )

)*/
<br><font color="#e78608">------解决方案--------------------</font><br>组合问题,可参考<br>
Copy after login
PHP code

/**C(M,N) recursive*/
function C($m,$n)
{
        if( $n 
                 
              
              
        
            
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template