pat 1063

一点教训

平方级别的算法会有一组数据超时

注意到nt的求解其实没必要动用俩个循环的,啧啧,滴水不漏啊

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
#define MAX 60
#define MAY 1000000010
using namespace std;
int n;
set<int> total[MAX];
//bool hashTab[MAX][MAY];
vector<double> res;
void calSim(int a , int b)
{
double nt = total[a].size();
set<int> sum;
set<int>::iterator it;
/*for(it = total[a].begin() ; it != total[a].end() ; ++it)
{
sum.insert((*it));
}
for(it = total[b].begin() ; it != total[b].end() ; ++it)
{
sum.insert((*it));
}
nt = sum.size();*/
double cnt = 0;
for(it = total[b].begin() ; it != total[b].end() ; ++it)
{
if(total[a].find((*it)) != total[a].end())
{
++cnt;
}
else
{
nt++;
}
}
double result = cnt / nt * 100;
res.push_back(result);
sum.clear();
//printf("%.1f%%\n" , cnt / nt * 100);
}
int main()
{
scanf("%d" , &n);
int i ,j , num;
// fill(hashTab[0] , hashTab[0] + MAX*MAY , false);
for(i = 1 ; i <= n ; ++i)
{
scanf("%d" , &num);
set<int> item;
for(j = 0 ; j < num ; ++j)
{
int mem;
scanf("%d" , &mem);
// hashTab[i][mem] = true;
item.insert(mem);
}
total[i] = item;
}
int cmpNum;
scanf("%d" , &cmpNum);
for(i = 0 ; i < cmpNum ; ++i)
{
int a , b;
scanf("%d%d" , &a , &b);
calSim(a , b);
}
for(i = 0 ; i < res.size() ; ++i)
{
printf("%.1f%%\n" , res[i]);
}
return 0;
}

热评文章