如何在PHP中生成(x)字符串(y)行的所有可能的行排列? [关闭]

如何在PHP中生成(x)字符串(y)行的所有可能的行排列?  [关闭]

问题描述:

I am trying to write a function that takes the following 2 parameters:

  1. A sentence as a string
  2. A number of lines as an integer

So if I was to call formatLines("My name is Gary", 2); ...

The possible outcomes would be:

  • array("My name is", "Gary");
  • array("My name", "is Gary");
  • array("My", "name is Gary");

It would return: array("My name", "is Gary"); because the difference in character counts for each line is as small as possible.

So the part I am ultimately stuck on is creating an array of possible outcomes where the words are in the correct order, split over x lines. Once I have an array of possible outcomes I would be fine working out the best result.

So how would I go about generating all the possible combinations?

Regards

Joe

我正在尝试编写一个带有以下2个参数的函数: p>

  1. 一个句子作为一个字符串 li>
  2. 许多行作为整数 li> ol>

    所以如果我要打电话 formatLines(“我的名字是加里”,2); strong> ... p>

    可能的结果是: p>

    • 数组(“我的名字是”,“加里”); li>
    • 数组(“我的名字”,“是加里”); li>
    • 数组 (“我的”,“名字是加里”); li> ul>

      它将返回:数组(“我的名字”,“是加里”); 因为每一行的字符数的差异尽可能小。 p>

      所以我最终坚持的部分是创建一个可能的结果数组,其中的单词在 正确的顺序,分成x行。 一旦我有一系列可能的结果,我会很好地计算出最好的结果。 p>

      那么我将如何生成所有可能的组合呢? p>

      问候 p>

      Joe p > div>

It seems like doing this by creating all possible ways of splitting the text and then determining the best one would be unnecessarily inefficient. You can count the characters and divide by the number of lines to find approximately the right number of characters per line.

function lineSplitChars($text, $lines) {
    if (str_word_count($text) < $lines) {
        throw new InvalidArgumentException('lines must be fewer than word count', 1);
    }

    $width = strlen($text) / $lines;                        // initial width calculation

    while ($width > 0) {

        $result = explode("
", wordwrap($text, $width));   // generate result

        // check for correct number of lines. return if correct, adjust width if not
        $n = count($result);
        if ($n == $lines) return $result;
        if ($n > $lines) {
            $width++;
        } else {
            $width--;
        };
    }
}

An answer has been accepted here - but this strikes me as a rather cumbersome method for solving the problem when PHP already provides a wordwrap() function which does most of the heavy lifting:

 function format_lines($str, $lines)
 {
     $guess_length=(integer)(strlen($str)/($lines+1));
     do {
         $out=explode("
", wordwrap($str, $guess_length));
         $guess_length++;
     } while ($guess_length<strlen($str) && count($out)>$lines);
     return $out;
 }

As it stands, it is rather a brute force method, and for very large inputs, a better solution would use optimum searching (adding/removing a larger initial interval then decreasing this in iterations)