dourong9253 2015-05-28 23:38
浏览 96
已采纳

如何从字符串中获得所需长度的所有排列?

I have a function where I can pass a string and a desired length to get all permutations with a fixed length from the characters of the string.

But now I want the permutations of entire words, e.g.

$source = "apple,potatoes,orange";

Sadly this function only gives me the permutations with characters and not entire words and I don't know how to modify the code, so I would get these permutations out of the above example data:

apple,apple
apple,potatoes
apple,orange
potatoes,apple
potatoes,potatoes
potatoes,orange
//...

Code:

<?php 

$source = 'AaBbCcDdEe'; 

foreach(combos_with_repetition($source, 2) as $combo) { 
    echo "$combo<br>
"; 
} 

function combos_with_repetition($input, $combo_len = 2) 
{ 
    for($i = 0; $i < $combo_len; ++$i) 
    { 
        @$part1 .= 'for($k'.$i.' = 0, $len = strlen($input); $k'.$i.' < $len; ++$k'.$i.') '; 
        @$part2 .= ($i?'.':'') . '$input[$k'.$i.']'; 
    } 
    eval($part1.'$rtn[] = '.$part2.';'); 
    return $rtn; 
} 

?>

So any help or hints how to modify the code would help.

  • 写回答

2条回答 默认 最新

  • duannan4486 2015-05-28 23:59
    关注

    This should work for you and even without evil().

    So what does this code do?

    1. How many permutations are there?

    Pretty simple:

    nl = amount of permutations

    Where n is the amount of words and l the desired length of each combination.

    So for this specific example there are 3 words (apple, patatoes and orange) and we want each permutation with a length of 3. Means:

    33 = 27 permutations

    2. Getting all permutations together

    We loop through all our permutations, which we already have(Starting off with one permutation, an "empty permutation" ($permutations = [[]];)), and for each permutation we go through our data array and combine each permutation with each input data to a new permutation.

    Now we do this until we get the desired length for each permutation.

    2.1 Example

    Input data:
    
    [1, 2]     //Input array with the data
    length = 2 //Desired length for each permutation
    
                                   //↓ new permutations for the next iteration
                                   │
    iteration 0:
    
        Permutations:
                      - []         │  -> []
                                      │
    iteration 1:        ┌─────────────┤
                        │             │
        Permutations:   v             v
                      - []    + 1  │  -> [1]  
                      - []    + 2  │  -> [2]   
                                      │
    iteration 2:        ┌─────────────┤
                        │             │
        Permutations:   v             v
                      - []    + 1  │  -> [1]
                      - []    + 2  │  -> [2]
                      - [1]   + 1  │  -> [1,1]  //desired length 2
                      - [1]   + 2  │  -> [1,2]  //desired length 2
                      - [2]   + 1  │  -> [2,1]  //desired length 2 
                      - [2]   + 2  │  -> [2,2]  //desired length 2
                                   //↑ All permutations here
    

    So as you can see in the above example we now have all permutations with the desired length which we want, here 2.

    But to get only the permutations with the desired length we are overwriting the result array each iteration, so that at the end only the permutations with the expected length are in the results array.

    3. Code:

    <?php
    
        function getPermutations($input = [], $length = 2, $delimiter = ",") {
            $permutations = [[]];
            $data = is_array($input) ? $input : explode($delimiter, $input);
    
            for ($count = 0; $count < $length; $count++) {
                $tmp = [];
                foreach ($permutations as $permutation) {
                    foreach ($data as $inputValue)
                        $tmp[] = array_merge($permutation, [$inputValue]);
    
                }
                $permutations = $tmp;
            }
    
            return $permutations;
    
        }
    
    
        $result = getPermutations("apple,patatoes,orange", 3);
        print_r($result);
    
    ?>
    

    output:

    Array
    (
        [0] => Array
            (
                [0] => apple
                [1] => apple
                [2] => apple
            )
        //...
        [26] => Array
            (
                [0] => orange
                [1] => orange
                [2] => orange
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料