给定一个字符串:
String words = "Mary had a little lamb";
如何在保持原始句子中单词出现顺序的同时获得句子片段的组合?
例:
{'Mary had a little lamb'} {'Mary had a little', 'lamb'} {'Mary had a', 'little lamb'}, {'Mary had a', 'little', 'lamb'} {'Mary had', 'a little lamb'}, {'Mary had', 'a little', 'lamb'}, {'Mary had', 'a', 'little lamb'}, {'Mary had', 'a', 'little', 'lamb'} {'Mary', 'had a little lamb'}, {'Mary', 'had a little', 'lamb'}, {'Mary', 'had a', 'little lamb'} and so on...
提前致谢 :)
这样考虑:
Mary <1> had <2> a <3> little <4> lamb
每个<number>s可以为true或false。如果为真,则在该位置剪切句子。
<number>
因此,如果您有n + 1个单词,那么问题就可以简化为对n位数字进行二进制表示,即从0到2 ^ n-1
例子:
0110 -> {'Mary had', 'a', 'little lamb'} 1111 -> {'Mary', 'had', 'a', 'little', 'lamb'} 0001 -> {'Mary had a little', 'lamb'} 1011 -> {'Mary', 'had a', 'little', 'lamb'}