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

The frequent subset problem is defined as follows. Suppose UUU={1, 2,…\ldots,N} is the universe, and S1S_{1}S1,S2S_{2}S2,…\ldots,SMS_{M}SM are MMM sets over UUU. Given a positive constant α\alphaα,0<α≤10<\alpha \leq 10<α1, a subset BBB (B≠0B \neq 0B0) is α-frequent if it is contained in at least αM\alpha MαM sets of S1S_{1}S1,S2S_{2}S2,…\ldots,SMS_{M}SM, i.e. ∣{i:B⊆Si}∣≥αM\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, letU={1,2,3,4,5}U=\{1, 2,3,4,5\}U={1,2,3,4,5},M=3M=3M=3,α=0.5\alpha =0.5α=0.5, and S1={1,5}S_{1}=\{1, 5\}S1={1,5},S2={1,2,5}S_{2}=\{1,2,5\}S2={1,2,5},S3={1,3,4}S_{3}=\{1,3,4\}S3={1,3,4}. Then there are 333 α-frequent subsets of UUU, which are {1}\{1\}{1},{5}\{5\}{5} and {1,5}\{1,5\}{1,5}.

Input Format

The first line contains two numbers NNN and α\alpha α, where NNN is a positive integers, and α\alphaα 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., linei+1i + 1i+1 contains SiS_{i}Si,1≤i≤M1 \le i \le M1iM . Your program should be able to handle NNN up to 202020 and MMM up to 505050.

Output Format

The number of α\alphaα-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表示集合U是从1~n,一个频率0<α<=1
M个集合,M不是给你的要自己统计,最后让你找这样的子集:
在M个集合中出现次数 >= M*α的

题解:
枚举子集,看其在M个集合中出现的次数

优化:
1.如何存这个M个集合?
用二进制的思想,第几位表示数值为几的数在集合中存在
例如用 0011010 表示1、3、4属于集合Si
那么这个数x存在就可以表示为 1<<x
最大的数是20,也就是说最多21位,最大数值是2^21,long long轻松处理
于是我们得到了M个longlong型的整数,这些数在计算机中都是以01串存在的

2. 如何枚举子集?
for(i = 1;i<=U;++i){if(i & Sj)==i,i是U的一个子集}

例如i在计算机中二进制表示为101,Sj在计算机表示为1101
如果i是Sj的子集那么按位与出来的结果还是i
如果结果 != i 说明存在某些位置i是1 Sj是0 即集合i中存在该元素,集合Sj不存在


枚举子集 也不过2^21 * 50 应该超不了一秒,不过这里还可以做一个优化

3.我们统计所有元素出现的总次数,如果<M*α,那么所求子集中一定不会出现这些元素。
于是我们得到了一个新集合tempans ,枚枚举该集合的子集即可
即 for(i = 1;i<=tempans;++i){
    if(i 属于 集合tempans && i 属于 集合Sj && i是M*α个以上集合的子集)
    then ans++;

}

4.读入用cin的io流处理
*/

#include <bits/stdc++.h>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
long long S[55];
int cnt[30];
int main()
{
    string s;
    int n;
    double a;

    cin>>n>>a;
    getchar();
    int m = 0;
    while(getline(cin,s)){

        stringstream ss;
        ss << s; ///从s读进ss
        int tmp;
        while(ss>>tmp){ ///从ss依次提取
           S[m] += (1 << tmp);
           cnt[tmp] ++;
        }
        m++;
    }

   int minnum;
    if(a*m - int(a*m) > 0.0000001) minnum = ceil(a*m);
        else minnum = a*m;

    long long tempans = 0;
    for(int i = 0;i<30;++i){
        if(cnt[i]>=minnum) tempans += (1<<i);
    }

    long long ans = 0;
    for(int i = 1;i<=tempans;++i){

        if((i & tempans)!=i) continue;
        long long temp = 0;
        for(int j = 0;j<m;++j){
            if((i & S[j]) == i)
                temp++;
            if(temp >= minnum) {
                ans++;break;
            }
        }
    }
    printf("%lld\n",ans);


    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值