找到了一套两套整数所有成对的OR [英] Finding a set of all pair-wise ORs of two sets of integers

查看:147
本文介绍了找到了一套两套整数所有成对的OR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于两组,每个都包含整数值,何以找到一组包含所有可能的成对 OR值这两个组的值?例如。 (所有的数字都是二进制)

Given two sets, each containing integer values, how could one find a set containing all possible pair-wise ORs of the values of those two sets? E.g. (all numbers are binary)

{1, 10} x {100, 1000} = {101, 1001, 110, 1010}
{1, 10} x {11, 101} = {11, 101, 111}

第一个例子结果完全四种组合,而第二个唯一的结果以3,由于这两组共享某些比特。明显的结果,可以计算在 O(米* n)的的,但有解决这个的更快的方法,考虑到在许多情况下,结果的大小会小于 M *ñ

The first example results in full four combinations, while the second one only results in three, since both sets share some bits. Obviously the result can be calculated in O(m*n), but is there a faster way of solving this, taking into account that in many cases the size of the result would be less than m*n?

在某些情况下,生成一组比 M *ñ显著小(如 {1,11,111}×{10, 110} = {11,111} ) - 但我不能完全查明所有这些案件的确切性质在一个通用的,足以方式获得的算法。理想情况下,应在 O(R)执行,其中研究的结果集的大小。可能有一些方式来分割源集合,集结结果采用动态规划方法,或做其他事的这种精神,但现在我找不到了。

In some of the cases the resulting set is significantly smaller than m*n (e.g. {1, 11, 111} x {10, 110} = {11, 111}) - but I can't quite pinpoint the exact nature of all those cases in a generic-enough way to get an algorithm. Ideally it should run in O(r), where r is the size of the resulting set. There may be some way to partition source sets, build-up the result using a dynamic programming approach, or do something else in that vein, but for now I can't find it.

推荐答案

虽然我不很了解,我不知道,这取决于数据的基数,采用的特里为一组可以提高工作效率。当遍历另一个设为与索引集,树的整个分支可以,如果它能够判定位中索引集合相匹配的当前整数跳过

Although I don't know much about it, I wonder if, depending on the cardinality of the data, using a trie for one set could improve efficiency. When traversing the other set to or with the indexed set, whole branches of the tree may be skipped if it can be determined that bits are matched for the current integer in the indexed set.

另一个优化可能是跳过比特长度的全部配对ñ如果我们已经有2 ^(N-1),该长的结果;例如,有八个可能的整数,4位长。

Another optimization could be to skip all pairings of bit-length n if we already have 2^(n-1) results of that length; for example, there are eight possible integers, four bits in length.

这篇关于找到了一套两套整数所有成对的OR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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