计蒜客 Frequent Subsets Problem&&2017 Icpc南宁赛

来源:互联网 发布:gephi数据分析案例 编辑:程序博客网 时间:2024/06/05 05:12

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.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 亚洲区(南宁赛区)网络赛


解:比赛没看,后来说用状态压缩,看了一下相关知识,把每一个所给子集用二进制的方法存起来(用右移运算符),比如说全集为1,2,3,4;

那么如果给你的集合为1,3,4,你把(1<<1)+(1<<3)+(1<<4)存到一个数组里,相当于存了1101,从右到左表示1在,2不在,3在,4在,然后利用&运算性质便可求解;

代码如下:

#include <iostream>#include <string.h>#include <algorithm>#include <math.h>using namespace std;int main(){int  a[100];memset(a,0,sizeof(a));int i=0,n,ant=0,sum=0,k=0,v;char c;double  m;cin>>n>>m;while (scanf("%d%c",&v,&c)!=EOF){a[k]+=(1<<(v-1));if (c=='\n')k++;}int va=ceil(m*k);for (int i=1;i<=(1<<n);i++){for (int j=0;j<k;j++)if ((i&a[j])==i)sum++;        if (sum>=va)        ant++;        sum=0;}cout<<ant<<endl; } 

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