2017 ACM-ICPC 亚洲区(南宁赛区)网络赛: M. Frequent Subsets Problem(状压)

 

The frequent subset problem is defined as follows. Suppose U={1, 2,…,N} is the universe, and S​1​​, S​2​​,…,SM​​ are M sets over U. Given a positive constant α, 0<α≤1, a subset B (B≠0) is α-frequent if it is contained in at least αM sets of S​1​​, S​2​​,…,SM​​, i.e. \left | \left \{ i:B\subseteq S_{i} \right \} \right | \geq \alpha M∣{i:BSi​​}∣≥αM. The frequent subset problem is to find all the subsets that are α-frequent. For example, let U={1,2,3,4,5}, M=3, α=0.5, and S​1​​={1,5}, S​2​​={1,2,5}, S​3​​={1,3,4}. Then there are 3 α-frequent subsets of U, which are {1},{5} and {1,5}.

Input Format

The first line contains two numbers N and α, where N is a positive integers, and α is a floating-point number between 0 and 1. Each of the subsequent lines contains a set which consists of a sequence of positive integers separated by blanks, i.e., line i + 1 contains Si​​, 1≤iM . Your program should be able to handle N up to 20 and M up to 50.

Output Format

The number of α-frequent subsets.

样例输入

15 0.4
1 8 14 4 13 2
3 7 11 6
10 8 4 2
9 3 12 7 15 2
8 3 2 4 5

样例输出

11

思路:每个集合可以用一个n位的二进制数表示。

 

 

#include<bits/stdc++.h>
using namespace std;
int a[55];
int main()
{
    memset(a,0,sizeof a);
    int m=0,n;
    double p;
    scanf("%d%lf",&n,&p);
    int num;
    char ch;
    while(scanf("%d%c",&num,&ch)!=EOF)
    {
        a[m]+=(1<<(num-1));
        if(ch=='\n')m++;
    }
    int ans=0;
    int sta=ceil(m*p);
    for(int i=1;i<(1<<n);i++)
    {
        int sum=0;
        for(int j=0;j<m;j++)
        {
            if((i&a[j])==i)sum++;
        }
        if(sum>=sta)ans++;
    }
    cout<<ans<<endl;
    return 0;
}

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值