串组合,同时保持令词中 [英] Combinations Of String While Maintaining Order Of Words

查看:99
本文介绍了串组合,同时保持令词中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个字符串:

String words = "Mary had a little lamb";

如何获得句子片段的组合而发生的话,在原句的顺序保持???

how to obtain a combination of sentence fragments while the order of occurrence of words in the original sentence is maintained ???

例如:

{'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...

在此先感谢:)

Thanks in advance :)

推荐答案

想想看这样的:

Mary <1> had <2> a <3> little <4> lamb

所有这些&LT;数量&GT; S可以是真或假。如果这是真的,那么你就砍了一句在该位置。

Each of these <number>s can be either true or false. If it is true, then you cut the sentence in that location.

因此​​,如果有n + 1个词,你的问题得到减少到经历具有n位数字的二进制重新presentation,即从0到2 ^ n-1个

So, if you have n+1 words, your problem gets reduced to going through binary representation of numbers with n bit, that is from 0 to 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'}

这篇关于串组合,同时保持令词中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆