Php 显示字符串的可能组合
我试图获取一个字符串并显示它的可能组合(在PHP中),但同时按每个单词的顺序说。例如:“你好”将返回(一个数组)Php 显示字符串的可能组合,php,combinations,Php,Combinations,我试图获取一个字符串并显示它的可能组合(在PHP中),但同时按每个单词的顺序说。例如:“你好”将返回(一个数组) 我现在的代码显示了所有的组合,但我希望它保持它们的顺序,而不是翻转单词。有人有什么想法或片段想分享吗?谢谢设置两个迭代器并打印它们之间的所有内容。比如说: <? $str = "How are you"; $words = explode(" ",$str); $num_words = count($words); for ($i = 0; $i < $num_words
我现在的代码显示了所有的组合,但我希望它保持它们的顺序,而不是翻转单词。有人有什么想法或片段想分享吗?谢谢设置两个迭代器并打印它们之间的所有内容。比如说:
<?
$str = "How are you";
$words = explode(" ",$str);
$num_words = count($words);
for ($i = 0; $i < $num_words; $i++) {
for ($j = $i; $j < $num_words; $j++) {
for ($k = $i; $k <= $j; $k++) {
print $words[$k] . " ";
}
print "\n";
}
}
?>
设置两个迭代器并打印它们之间的所有内容。比如说:
<?
$str = "How are you";
$words = explode(" ",$str);
$num_words = count($words);
for ($i = 0; $i < $num_words; $i++) {
for ($j = $i; $j < $num_words; $j++) {
for ($k = $i; $k <= $j; $k++) {
print $words[$k] . " ";
}
print "\n";
}
}
?>
我知道这是一个很老的帖子,但另一个答案不是很灵活,所以我想我会带来一个新的答案
解释 因此,您正在寻找以下所有组合: (2n)-1 在您的具体示例中,哪一个是: (23)-1=(8)-1=7 那么我现在如何得到所有的组合呢?我们循环遍历我们已经拥有的所有组合(从一个组合开始,一个“空组合”(
$results=[[]];
)),对于每个组合,我们遍历数组中的下一个单词,并将每个组合与每个新词组合成一个新组合
示例
Array with the words/numbers (Empty array is '[]'):
[1, 2, 3]
因此,正如您所看到的,总有:(2^n)-1
组合。此外,从这个方法中,组合数组中还剩下一个空数组,因此在返回数组之前,我只使用它删除所有空数组并重新为整个数组编制索引
密码
我知道这是一个很老的帖子,但另一个答案不是很灵活,所以我想我会带来一个新的答案
解释 因此,您正在寻找以下所有组合: (2n)-1 在您的具体示例中,哪一个是: (23)-1=(8)-1=7 那么我现在如何得到所有的组合呢?我们循环遍历我们已经拥有的所有组合(从一个组合开始,一个“空组合”(
$results=[[]];
)),对于每个组合,我们遍历数组中的下一个单词,并将每个组合与每个新词组合成一个新组合
示例
Array with the words/numbers (Empty array is '[]'):
[1, 2, 3]
因此,正如您所看到的,总有:(2^n)-1
组合。此外,从这个方法中,组合数组中还剩下一个空数组,因此在返回数组之前,我只使用它删除所有空数组并重新为整个数组编制索引
密码
问题的答案。有必要获得所有可能的数组元素组合并存储以下内容
<?php
$alphabet = array('a','b','c');
$result = array();
$arrResult = array();
// recursively create all possible combinations {
combine($alphabet, $result, $arrResult);
function combine($shiftedAlphabet, &$result, &$arrResult) {
global $alphabet;
$currentElementStr = '';
$currentElementArr = array();
for($i = 0; $i < count($shiftedAlphabet); ++$i) {
$newElement = $shiftedAlphabet[$i];
$currentElementStr .= $newElement;
$currentElementArr[] = $newElement;
if(!in_array($currentElementStr, $result)) { // if not duplicated => push it to result
// find right position {
$thisCount = count($currentElementArr);
$indexFrom = 0;
$indexToInsert = 0;
// find range of indexes with current count of elements {
foreach ($arrResult as $arrResultKey => $arrResultValue) {
$indexToInsert = $arrResultKey + 1;
if ($thisCount > count($arrResultValue)) {
$indexFrom = $indexToInsert;
}
if ($thisCount < count($arrResultValue)) {
--$indexToInsert;
break;
}
}
// find range of indexes with current count of elements }
// find true index inside true range {
$trueIndex = $indexToInsert;
$break = false;
for($j = $indexFrom; $j < $indexToInsert; ++$j) {
$trueIndex = $j + 1;
foreach($arrResult[$j] as $key => $value) {
if (array_search($value, $alphabet) > array_search($currentElementArr[$key], $alphabet)) {
$break = true;
break;
}
}
if($break) {
--$trueIndex;
break;
}
}
// find true index inside true range }
array_splice($result, $trueIndex, 0, $currentElementStr);
array_splice($arrResult, $trueIndex, 0, array($currentElementArr));
}
}
for($i = 0; $i < count($shiftedAlphabet) - 1; ++$i) {
$tmpShiftedAlphabet = $shiftedAlphabet; // for combining all possible subcombinations
array_splice($tmpShiftedAlphabet, $i, 1);
combine($tmpShiftedAlphabet, $result, $arrResult);
}
}
// recursively create all possible combinations }
var_dump($result);
?>
示例结果问题的答案。有必要获得所有可能的数组元素组合并存储以下内容
<?php
$alphabet = array('a','b','c');
$result = array();
$arrResult = array();
// recursively create all possible combinations {
combine($alphabet, $result, $arrResult);
function combine($shiftedAlphabet, &$result, &$arrResult) {
global $alphabet;
$currentElementStr = '';
$currentElementArr = array();
for($i = 0; $i < count($shiftedAlphabet); ++$i) {
$newElement = $shiftedAlphabet[$i];
$currentElementStr .= $newElement;
$currentElementArr[] = $newElement;
if(!in_array($currentElementStr, $result)) { // if not duplicated => push it to result
// find right position {
$thisCount = count($currentElementArr);
$indexFrom = 0;
$indexToInsert = 0;
// find range of indexes with current count of elements {
foreach ($arrResult as $arrResultKey => $arrResultValue) {
$indexToInsert = $arrResultKey + 1;
if ($thisCount > count($arrResultValue)) {
$indexFrom = $indexToInsert;
}
if ($thisCount < count($arrResultValue)) {
--$indexToInsert;
break;
}
}
// find range of indexes with current count of elements }
// find true index inside true range {
$trueIndex = $indexToInsert;
$break = false;
for($j = $indexFrom; $j < $indexToInsert; ++$j) {
$trueIndex = $j + 1;
foreach($arrResult[$j] as $key => $value) {
if (array_search($value, $alphabet) > array_search($currentElementArr[$key], $alphabet)) {
$break = true;
break;
}
}
if($break) {
--$trueIndex;
break;
}
}
// find true index inside true range }
array_splice($result, $trueIndex, 0, $currentElementStr);
array_splice($arrResult, $trueIndex, 0, array($currentElementArr));
}
}
for($i = 0; $i < count($shiftedAlphabet) - 1; ++$i) {
$tmpShiftedAlphabet = $shiftedAlphabet; // for combining all possible subcombinations
array_splice($tmpShiftedAlphabet, $i, 1);
combine($tmpShiftedAlphabet, $result, $arrResult);
}
}
// recursively create all possible combinations }
var_dump($result);
?>
示例结果效率低下的情况比比皆是$str=“你好吗”$单词=爆炸(“,$str”)$总数=计数($words);对于($i=0;$i<$total_words;$i++){for($j=$i+1;$j<$total_words;$j++){for($k=$i;$k<$j;$k++){print$words[k]。“;}print”\n”}如果你不想要额外的空间并且想要结果是一个数组,只需
将这个$words[$k]
推到一个数组中,然后内爆,将结果添加到要返回的数组中。当字符串包含4+个字时,此方法不起作用,例如:“a b c d”-将不会返回“a c d”组合无效$str=“你好吗”$单词=爆炸(“,$str”)$总数=计数($words);对于($i=0;$i<$total_words;$i++){for($j=$i+1;$j<$total_words;$j++){for($k=$i;$k<$j;$k++){print$words[k]。“;}print”\n”}如果你不想要额外的空间并且想要结果是一个数组,只需将这个$words[$k]
推到一个数组中,然后内爆,将结果添加到要返回的数组中。当字符串包含4个以上的单词时,此方法不起作用,例如:“a b c d”-将不会返回“a c d”组合您正在使用的代码是什么?您正在使用的代码是什么?但是“how you”是错误的,它跳过了中间的一个单词。应该按照OPA从左到右排列,但“how you”是错误的,它跳过了中间的一个词。应仅按照OP从左到右
how
are
how are
you
how you
are you
how are you
<?php
$alphabet = array('a','b','c');
$result = array();
$arrResult = array();
// recursively create all possible combinations {
combine($alphabet, $result, $arrResult);
function combine($shiftedAlphabet, &$result, &$arrResult) {
global $alphabet;
$currentElementStr = '';
$currentElementArr = array();
for($i = 0; $i < count($shiftedAlphabet); ++$i) {
$newElement = $shiftedAlphabet[$i];
$currentElementStr .= $newElement;
$currentElementArr[] = $newElement;
if(!in_array($currentElementStr, $result)) { // if not duplicated => push it to result
// find right position {
$thisCount = count($currentElementArr);
$indexFrom = 0;
$indexToInsert = 0;
// find range of indexes with current count of elements {
foreach ($arrResult as $arrResultKey => $arrResultValue) {
$indexToInsert = $arrResultKey + 1;
if ($thisCount > count($arrResultValue)) {
$indexFrom = $indexToInsert;
}
if ($thisCount < count($arrResultValue)) {
--$indexToInsert;
break;
}
}
// find range of indexes with current count of elements }
// find true index inside true range {
$trueIndex = $indexToInsert;
$break = false;
for($j = $indexFrom; $j < $indexToInsert; ++$j) {
$trueIndex = $j + 1;
foreach($arrResult[$j] as $key => $value) {
if (array_search($value, $alphabet) > array_search($currentElementArr[$key], $alphabet)) {
$break = true;
break;
}
}
if($break) {
--$trueIndex;
break;
}
}
// find true index inside true range }
array_splice($result, $trueIndex, 0, $currentElementStr);
array_splice($arrResult, $trueIndex, 0, array($currentElementArr));
}
}
for($i = 0; $i < count($shiftedAlphabet) - 1; ++$i) {
$tmpShiftedAlphabet = $shiftedAlphabet; // for combining all possible subcombinations
array_splice($tmpShiftedAlphabet, $i, 1);
combine($tmpShiftedAlphabet, $result, $arrResult);
}
}
// recursively create all possible combinations }
var_dump($result);
?>