2013-10-02 58 views
0

我正在尝试编写一个函数,让我们可以说“hey there”,然后创建该字符串的所有前缀的数组。所以它会返回“h”,“he”,“hey”,“hey”,“hey t”等。获取字符串的所有可能组合(preffix/suffix)

然后我想要创建所有后缀的第二个数组字符串)。所以对于相同的字符串,它会返回“e”,“呃”,“ere”,“ereh”,“ereht”,“ereht”等

我想要弄清楚这一点,但我已经管理得到下面这个获得一个字符串的所有可能的组合,我只需要让它做到这一点,只有按顺序。

$str = "hey there"; 

function permute($str,$i,$n) { 
    if ($i == $n) 
     print "$str\n"; 
    else { 
     for ($j = $i; $j < $n; $j++) { 
      swap($str,$i,$j); 
      permute($str, $i+1, $n); 
      swap($str,$i,$j); // backtrack. 
     } 
    } 
} 

// function to swap the char at pos $i and $j of $str. 
function swap(&$str,$i,$j) { 
    $temp = $str[$i]; 
    $str[$i] = $str[$j]; 
    $str[$j] = $temp; 
} 

permute($str,0,strlen($str)); // call the function. 

} 

任何帮助非常感谢。

+1

substr,strrev和循环的组合将做的伎俩。 – Virus721

+0

不是重复的,我不想生成像在帖子中所述的所有组合。请在评论之前阅读它,我看到我将检查这些功能谢谢你。 –

+0

几乎不值得回答:'函数permute($ string){length = strlen($ string); $ result = array();对于($ i = 1; $ i <= $ length; $ i ++){ $ result [] = substr($ string,0,$ i); } return $ result; } function permuteboth($ string){ $ results = array(); $ results [] = permute($ string); $ results [] = permute(strrev($ string)); return $ results; } $ str =“hey there”; $ results = permuteboth($ str); var_dump($ results); ' –

回答

2

这是你想要做什么?

<?php 
    function getPrefixSuffix($string, &$prefixes = array(), &$suffixes = array()) { 
     $stringLength = strlen($string); 
     for ($i = 1; $i < $stringLength; $i++) { 
      $prefixes[] = substr($string, 0, $i); 
     } 
     for ($i = $stringLength - 1; $i >= 1; $i--) { 
      $suffixes[] = strrev(substr($string, $i)); 
     } 
    } 

    getPrefixSuffix("hey there", $prefixes, $suffixes); 

    print_r($prefixes); 
    /* 
     Array 
     (
      [0] => h 
      [1] => he 
      [2] => hey 
      [3] => hey 
      [4] => hey t 
      [5] => hey th 
      [6] => hey the 
      [7] => hey ther 
     ) 
    */ 

    print_r($suffixes); 
    /* 
     Array 
     (
      [0] => e 
      [1] => er 
      [2] => ere 
      [3] => ereh 
      [4] => ereht 
      [5] => ereht 
      [6] => ereht y 
      [7] => ereht ye 
     ) 
    */ 
?> 

DEMO

0

会简单的for循环和一对substr的做同样的?

$prefixes = array(); 
$suffixes = array(); 
$str = "hey there"; 
$l = strlen($str); 
for($i=1; $i<$l; $i++) { 
    $prefixes[] = substr($str, 0, $i); 
    $suffixes[] = strrev(substr($str, $l-$i, $i)); 
} 

demo

+0

将'strrev'添加到'$ suffixes',因为OP希望它成为'ereht'而不是'there'。 – h2ooooooo

0

这应该工作。我写了3个函数。第一个只需在每个追加处打印所有前缀。第三个是你写的交换方法。第二个简单地倒换字符串,然后调用getPrefix。请注意,原始字符串保持不变。

$str = "hey there" 

function getPrefix($str) 
{ 
    $printString = ""; 
    for ($i = 0; $i < strlen($str); $i++) 
    { 
     $printString .= $str[$i]; 
     print ($printString); 
    } 
} 

function getBackwards($str) 
{ 
    if (strlen($str) % 2 == 0) 
    { 
     for ($i = 0; $i < strlen($str)/2; $i++) 
      swap ($str, $i, strlen($str)-1-$i); 
    } 
    else 
    { 
     for ($i = 0; $i < (strlen($str)-1)/2; $i++) 
      swap ($str, $i, strlen($str)-1-$i); 
    } 
    getPrefix($str); 
} 

function swap(&$str,$i,$j) 
{ 
    $temp = $str[$i]; 
    $str[$i] = $str[$j]; 
    $str[$j] = $temp; 
} 
相关问题