The frequent subset problem is defined as follows. Suppose U={1, 2,…,N} is the universe, and S1, S2,…,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 S1, S2,…,SM, i.e. ∣{i:B⊆Si}∣≥α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 S1={1,5}, S2={1,2,5}, S3={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≤i≤M . Your program should be able to handle N up to 20and 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 <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int n,m;
double p;
int num[100];
void read(char* str)
{
if(strcmp(str,"\n")==0 || strcmp(str,"") == 0)
return ;
int i = 0;
while(str[i] != '\0')
{
int t = 0;
while(str[i] == ' ')
i++;
while(str[i] != ' '&&str[i] != '\0')
{
t = t*10 + str[i] - '0';
i++;
}
num[m] += 1<<(t-1);
}
m++;
}
int main()
{
scanf("%d %lf",&n,&p);
m = 0;
char str[1000];
memset(num,0,sizeof(num));
while(gets(str))
{
read(str);
}
int k = ceil(p*m);
int ans = 0;
for(int i = 1; i<(1<<n); i++)
{
int tmp = 0;
for(int j = 0; j<m; j++)
{
if((num[j]&i) == i)
tmp++;
}
if(tmp>=k)
ans++;
}
printf("%d\n",ans);
return 0;
}