2013-10-02 59 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; 
} 
相關問題