计蒜客 Frequent Subsets Problem(状态压缩)

来源:互联网 发布:淘宝红包能抵用运费吗 编辑:程序博客网 时间:2024/06/05 09:12

The frequent subset problem is defined as follows. Suppose UU={1, 2,\ldots,N} is the universe, and S_{1}S1S_{2}S2,\ldots,S_{M}SMare MM sets over UU. Given a positive constant \alphaα0<\alpha \leq 10<α1, a subset BB (B \neq 0B0) is α-frequent if it is contained in at least \alpha MαM sets of S_{1}S1S_{2}S2,\ldots,S_{M}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\}U={1,2,3,4,5}M=3M=3\alpha =0.5α=0.5, and S_{1}=\{1, 5\}S1={1,5}S_{2}=\{1,2,5\}S2={1,2,5}S_{3}=\{1,3,4\}S3={1,3,4}. Then there are 33 α-frequent subsets of UU, which are \{1\}{1},\{5\}{5} and \{1,5\}{1,5}.

Input Format

The first line contains two numbers NN and \alphaα, where NN 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., line i + 1i+1 contains S_{i}Si1 \le i \le M1iM . Your program should be able to handle NN up to 2020 and MM up to 5050.

Output Format

The number of \alphaα-frequent subsets.

样例输入

15 0.41 8 14 4 13 23 7 11 610 8 4 29 3 12 7 15 28 3 2 4 5

样例输出

11

题目来源

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

#include<iostream>#include<stdio.h>#include<string.h>using namespace std;int s[55];int main(){memset(s,0,sizeof(s));int n,m=0;double a;scanf("%d%lf",&n,&a);int x;char p;while(scanf("%d%c",&x,&p)!=EOF){s[m]+=(1<<(x-1));if(p=='\n')m++;}int g=m*a+(bool)(m*a-(int)(m*a));     //代表至少为g个给定集合的子集 int ans=0;for(int i=1;i<(1<<n);i++)           //二进制表示状态,枚举所有集合 {int sum=0;for(int j=0;j<m;j++)            //跑遍所有给定m个集合判断是不是子集关系 {if((i&s[j])==i)sum++;}if(sum>=g)ans++;}printf("%d\n",ans);}





阅读全文
0 0
原创粉丝点击