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;
    }
  • 相关阅读:
    Twitter Storm安装配置(Ubuntu系统)单机版
    Ubuntu下安装配置JDK1.7
    JS性能优化
    JavaScript禁用页面刷新
    pomelo获取客户端IP
    MySQL数据库工具类之——DataTable批量加入MySQL数据库(Net版)
    MySQL5.6忘记root密码(win平台)
    清空文件下的SVN控制文件
    Windows平台搭建NodeJs开发环境以及HelloWorld展示—图解
    Unity3D默认的快捷键
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/6370710.html
Copyright © 2011-2022 走看看