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;
    }
  • 相关阅读:
    依赖注入(DI)和Ninject
    Dapper.NET——轻量ORM
    优化SQL查询:如何写出高性能SQL语句
    Razor语法
    sublime Text 3 官方版 3114 注册码
    数据库索引,存储过程,视图,事务
    Action向视图传值的6种方式
    C#知识点提要
    算法总结
    c++ 构造函数,拷贝构造函数,析构函数与赋值操作符
  • 原文地址:https://www.cnblogs.com/iwantstrong/p/6370710.html
Copyright © 2011-2022 走看看