zoukankan      html  css  js  c++  java
  • The Number of set_状态压缩

    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
     

    【题意】给出几个集合,可以一个或多个合成,求有多少种不同的集合

    【思路】用二进制表示,一个二进制表示就是一种状态,当前状态形成,从头遍历一遍,将存在的状态与他合成一个新的状态。

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    const int N=1<<15;
    
    int set[N];
    int main()
    {
        int n,m;
        int k,p,x;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            memset(set,0,sizeof(set));
            for(int i=0;i<n;i++)
            {
                scanf("%d",&k);
                p=0;
                for(int j=0;j<k;j++)
                {
                    scanf("%d",&x);
                    p+=1<<(x-1);
                }
                set[p]=1;//p状态的集合,标记为1,表示已经有了
                for (int j=1;j<N;j++)
                {
                    if(set[j])//如果j状态有了,可与p状态合并为p|j状态;
                        set[p|j]=1;
                }
            }
            int sum=0;
            for(int j=1;j<N;j++)
            {
                if(set[j])
                    sum++;
            }
            printf("%d
    ",sum);
        }
        return 0;
    }
  • 相关阅读:
    JavaScript对iframe的DOM操作
    如何处理JSON中的特殊字符
    JavaScript引擎是单线程的
    JSONP跨域的原理解析
    四道JavaScript面试题检测你的js基本功
    让IE6也支持position:fixed
    Keras Layer 的 call(x) 和 input_shape
    C++文件读写 fwrite 和 fread
    Windows Ubuntu 子系统修改默认登陆用户·
    Python新建文件夹(如果不存在)
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/6370710.html
Copyright © 2011-2022 走看看