在具有所有可能性的字符之间生成点

问题描述:

我正在寻找一种PHP中的算法来输出点的所有可能性。产生我们可以放在任何地方的词,但现在允许彼此重复两个点。例如, “注意” 输出象下面这样:在具有所有可能性的字符之间生成点

note 
n.ote 
n.o.te 
n.o.t.e 
no.t.e 
not.e 
n.ot.e 
.... 

,也低于输出是错误的:

n..ote (repeat dots right after each other) 
.note (put dots at first of word) 
note. (put dots at end of word) 
+0

是否有一个最大的努每个名字有多少点? –

+0

不,我需要所有的可能性作为输出,但必须验证两个点不相互位于一起 – user3221719

+1

因此,最大点将像字符串的长度 - 1.这是一条线索。 :) –

递归的方式:

Put current char of source in the result string 
if current char is the last one 
    output result 
else 
    call recursive function with the next char index 
    add dot to result and call recursive function with the next char index 

迭代的方式:

2^(Len-1)带点的组合,Len i字长。 做一个循环k = 0..2^(Len-1) - 1并在那些地方,其中k的二进制表示包含每k插入点1 S(K = 2 =二进制010 =>po.le

我找到了解决办法,最终通过的https://stackoverflow.com/users/844416/mbo有益的指导:

function stringInsert($str,$insertstr,$pos){ 
    $str = substr($str, 0, $pos) . $insertstr . substr($str, $pos); 
    return $str; 
} 

function generate($var="note",$i=0){ 
    $length = strlen($var); 

    while ($i+1 < $length) { 
     $i++; 
     $new = stringInsert($var,'.',$i); 
     echo $new; 
     generate($new,$i+1); 

    } 
} 


generate('shaghayegh'); 

例如关键字 “笔记” 产生7串

关键字 “shaghayegh” 产生511串