尽可能在字符之间生成点 [英] Generate dots between characters with all possibilities
问题描述
我正在寻找PHP中的算法,以使用点来输出所有可能性。产生的结果,我们可以将do放置在单词的任何位置,但现在允许在彼此之后重复两个点。例如 note输出,如下所示:
I am looking for an algorithm in PHP to make output of all possibilities with dot. produces we can put do in any place of word but repeated two dots after each other is now allowed. for example "note" output like below:
note
n.ote
n.o.te
n.o.t.e
no.t.e
not.e
n.ot.e
....
,并且下面的输出也是错误的:
and also below output is wrong:
n..ote (repeat dots right after each other)
.note (put dots at first of word)
note. (put dots at end of word)
推荐答案
递归方式:
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
)
There are 2^(Len-1)
combinations with dots, where Len i word length.
Make a loop for k = 0..2^(Len-1) - 1
and for every k insert dots in those places, where binary representation of k contains 1
s (k=2 = binary 010 = > po.le
)
这篇关于尽可能在字符之间生成点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!