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;
    }
  • 相关阅读:
    Android简易注解View(java反射实现)
    android注解[Jake Wharton Butter Knife]
    Android渠道汇总
    SVN管理规范
    马上着手开发iOS应用程序一 todolist
    MVC记录
    OC中intValue要注意的地方
    将从数组中取到的字符串赋值给了UIImage导致的错误
    一个null引发的错误
    记录一些容易忘记的属性 -- UITabBarController
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/6370710.html
Copyright © 2011-2022 走看看