从可能重复的字符列表中生成唯一组合
我正在寻找从元素列表生成组合。现在我正在使用一种发电装置的方法。例如,要从 {a,b,c} 生成组合,我将枚举 001,010,100,101 等...并获取相应二进制索引设置为 1 的元素。 但是当列表中有重复的字符比如 {a,a,b} 时,问题就出现了。上述方法将给出 a,a,b,ab,ba,aab。我只想看到 a,b,ab,aa,aab。
我正在考虑编写一些二进制掩码来消除重复的字符串,但没有成功。 关于如何生成独特的组合有什么想法吗?
I am looking to generate combinations from a list of elements. Right now i am using a approach of generating power set. For example to generate combinations from {a,b,c}, i will enumerate 001,010,100 ,101 etc...and take the element for which the corresponding binary index is set to 1.
But the problem comes when there are repeated characters in the list Say {a,a,b}. the above approach would give a,a,b,ab,ba,aab. where as i would like to see only a,b,ab,aa,aab.
I was thinking of writing some binary mask to eliminate repeated strings but was not succesfull.
Any thoughts on how to generate unique combinations ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以生成长度等于不同元素数量的正整数向量,而不是生成位向量,但受限于每个分量的范围可以从 0 到相应元素的重数。在上面的示例中,有两个不同的元素(a 和 b),其重数分别为 2 和 1。因此,你会得到
Rather than generate bit vectors, you can generate vectors of positive integers of length equal to the number of distinct elements, subject to the restriction that each component can range from 0 up to the multiplicity of the corresponding element. In your example above, there are two distinct elements (a and b) with multilpicities 2 and 1, respectively. Therefore, you would get