The Number of set_状态压缩&&位运算

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1338    Accepted Submission(s): 823


Problem Description
Given you n sets.All positive integers in sets are not less than 1 and not greater than m.If use these sets to combinate the new set,how many different new set you can get.The given sets can not be broken.
 
Input
There are several cases.For each case,the first line contains two positive integer n and m(1<=n<=100,1<=m<=14).Then the following n lines describe the n sets.These lines each contains k+1 positive integer,the first which is k,then k integers are given. The input is end by EOF.
 
Output
For each case,the output contain only one integer,the number of the different sets you get.
 
Sample Input
4 4 1 1 1 2 1 3 1 4 2 4 3 1 2 3 4 1 2 3 4
 
Sample Output
15 2

用二进制保存一个集合的元素,如集合为1 3,即为(101)5;

集合的合并(9)1001 | 111(7)=1111  就是15;最大就是(11111111111111)2^15-1来表示一个集合!

#include<iostream>
#include<string.h>
using namespace std;

int s[1<<15];
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        memset(s,0,sizeof(s));
        int cnt=0;
        while(n--)
        {
            int k,ele;
            int set=0;
            cin>>k;
            while(k--)
            {
                cin>>ele;
                set=set|(1<<(ele-1));//集合中有元素ele,在1后面添加ele-1个零,则表示第ele位为1表示集合中存在ele;
            }
            s[set]=1;
            for(int j=1;j<=(1<<14);j++)
            {
                if(s[j]) {s[set|j]=1;}
            }
        }
        for(int j=1; j<=(1<<14); j++)
        {
            if(s[j]) cnt++;
        }
        cout<<cnt<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/iwantstrong/p/5736647.html