givn m sets. some sets may be subsets of others. If one set is a subset of
another set, then this smaller set will be removed. Finally we have the sets
from which we can not remove any set. Assume the elements in all the sets
are
same type and only "==" operator is supported on these elements.
Is there any good algorithm to do this job ? Thanks.
g**********y 发帖数: 14569
2
基本想法:把Element映射到数字,然后用Bitmap来计算包容关系。
pesudo-code =>
foreach set in sets {
Bitmap b1 = transform(set);
boolean add = true;
foreach b2 in map.keySet() {
Bitmap b3 = b1.and(b2);
if (b3.equals(b2)) {
bitmaps.remove(b2);
}
else if (b3.equals(b1)) {
add = false;
}
}
if (add) map.put(b1, set);
}
return map.values();
r*******y 发帖数: 1081
3
So it is using hashing. But how to do hashing here is also a big deal, right
?
Thanks
I am not sure what's your question. But as example, here is a simplified
Java implementation, assume sets no more than 32. If more than that, you can
use BigInteger or write your own class.
public class Subset {
public Set[] merge(Set[] sets) {
HashMap