用位运算获得集合的所有子集
来源:互联网 发布:视频特效软件 编辑:程序博客网 时间:2024/05/22 02:28
引自 bmerry's bitmark tutorial in Topcoder:
All the subsets
A big advantage of bit manipulation is that it is trivial to iterate over all the subsets of an N-element set: every N-bit value represents some subset. Even better, if A is a subset of B then the number representing A is less than that representing B, which is convenient for some dynamic programming solutions. It is also possible to iterate over all the subsets of a particular subset (represented by a bit pattern), provided that you don't mind visiting them in reverse order (if this is problematic, put them in a list as they're generated, then walk the list backwards). The trick is similar to that for finding the lowest bit in a number. If we subtract 1 from a subset, then the lowest set element is cleared, and every lower element is set. However, we only want to set those lower elements that are in the superset. So the iteration step is just i = (i - 1) & superset.
这是我写的实验,三个元素的集合,111表示:
#include<iostream>
using namespace std; int main()
{ int bitNumber = 7;
while ((bitNumber-1)!=0)
{ bitNumber = (bitNumber-1);
printf("%d %d %d/n", bitNumber>>2, (bitNumber&2)>>1, bitNumber&1); } return 0;
}
- 用位运算获得集合的所有子集
- 位运算之求集合的所有子集
- 【Java】通过位运算求一个集合的所有子集
- 给力!简单!易懂!位运算之求集合的所有子集
- 通过位运算求一个集合的子集
- 利用位运算枚举所有子集
- ACM 利用位运算枚举所有子集
- 打印集合所有的子集
- 求集合的所有子集
- 打印集合的所有子集
- 一个集合的所有子集
- 求集合的所有子集
- 输出集合的所有子集
- 求集合的所有子集
- Leetcode031--集合的所有子集
- 求集合的所有子集
- 求集合的所有子集
- 求集合的所有子集的算法
- 照明设计原理
- 电脑蓝屏后该做的几件事情
- MS SQL SERVER中时间的精度问题
- c++中的字符数组
- Set irregular shape for windows form
- 用位运算获得集合的所有子集
- Widows 文件操作和内存映射
- swt-designer配置开发环境
- VC++快捷键大全
- 使用mysqlbinlog查看二进制日志的问题
- Oracle 精妙SQL语句集锦
- 估算数学常量e的值:e=1+1/1!+1/2!+1/3!+...
- Second life的源码销售方式
- C#聊天室