-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day-287.cpp
69 lines (69 loc) · 2.18 KB
/
Day-287.cpp
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
#include <bits/stdc++.h>
using namespace std;
using dcc= tuple<double, char, char>;
bool compare(dcc&a, dcc&b) {
return get<0>(a) > get<0>(b);
}
double getSimilarity(set<int>&a , set<int>&b) {
vector<int>inter(a.size()+b.size());
vector<int>::iterator l;
l = set_intersection(a.begin(), a.end(), b.begin(), b.end(), inter.begin());
double interE= (l - inter.begin());
l = set_union(a.begin(), a.end(), b.begin(), b.end(), inter.begin());
double uniE= (l - inter.begin());
return double(interE / uniE);
}
list<pair<char,char>> similarWebsites(multimap<char,int>website , int k) {
list<pair<char,char>>ans;
map<char,set<int>>user;
for (auto &itr: website) {
user[itr.first].insert(itr.second);
}
vector<char>web;
for (auto& itr: user) {
web.push_back(itr.first);
}
priority_queue<dcc, vector<dcc>, function<bool(dcc& ,dcc&)>>pq(compare);
for (int i=0; i<web.size(); ++i) {
for (int j=i+1; j<web.size(); ++j) {
double similarity = getSimilarity(user[web[i]] , user[web[j]]);
if (pq.size() < k) {
pq.push(make_tuple(similarity , web[i] , web[j]));
}else if (get<0>(pq.top()) < similarity) {
pq.pop();
pq.push(make_tuple(similarity , web[i] , web[j]));
}
}
}
while(!pq.empty()) {
ans.push_back(make_pair(get<1>(pq.top()), get<2>(pq.top())));
pq.pop();
}
return ans;
}
int main(void){
multimap<char,int>website;
website.insert({'a',1});
website.insert({'a',3});
website.insert({'a',5});
website.insert({'b',2});
website.insert({'b',6});
website.insert({'c',1});
website.insert({'c',2});
website.insert({'c',3});
website.insert({'c',4});
website.insert({'c',5});
website.insert({'d',4});
website.insert({'d',5});
website.insert({'d',6});
website.insert({'d',7});
website.insert({'e',1});
website.insert({'e',3});
website.insert({'e',5});
website.insert({'e',6});
list<pair<char,char>>l = similarWebsites(website , 3);
for(auto&itr:l) {
cout << itr.first << ' ' << itr.second << '\n';
}
return 0;
}