从可能重复的字符列表中生成唯一组合

发布于 2024-09-25 22:08:28 字数 239 浏览 0 评论 0原文

我正在寻找从元素列表生成组合。现在我正在使用一种发电装置的方法。例如,要从 {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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡墨 2024-10-02 22:08:28

您可以生成长度等于不同元素数量的正整数向量,而不是生成位向量,但受限于每个分量的范围可以从 0 到相应元素的重数。在上面的示例中,有两个不同的元素(a 和 b),其重数分别为 2 和 1。因此,你会得到

a b
-------
0 1 --> b
1 0 --> a
1 1 --> ab
2 0 --> aa
2 1 --> aab

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

a b
-------
0 1 --> b
1 0 --> a
1 1 --> ab
2 0 --> aa
2 1 --> aab
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文