l2-005. Set similarity time limit MS Memory limit 65536 KB code length limit 8000 B award Program StandardAuthor Chen
Given a collection of two integers, their similarity is defined as: nc/nt*100%. Where NC is the number of unequal integers in all two sets, NT is the number of unequal integers that have a total of two sets. Your task is to calculate the similarity of any given set.
Input format:
Enter the first line to give a positive integer n (<=50), which is the number of sets. The next n rows correspond to one set per line. Each set first gives a positive integer m (<=104), which is the number of elements in the set, followed by an integer in the M [0, 109] interval.
The line then gives a positive integer k (<=2000), followed by the K-line, each line corresponding to a pair of sets that need to calculate the similarity of the number (set from 1 to n number). The numbers are separated by a space.
Output format:
For each pair that needs to be computed, output their similarity in one row, which is the percentage number that retains the 2 digits after the decimal point.
Input Sample:
33 99 87 1014 87 101 5 877 99 101 18 5 135 18 9921 21 3
Sample output:
50.00%33.33%
Idea: The collection naturally think of the STL's associative container set, and then combine its usage to solve the problem perfectly.
1#include <bits/stdc++.h>2#include <Set>3 using namespacestd;4 intMain () {5 Set<int> s[ -];6 intN,m,x,k,y1,y2;7Cin>>N;8 for(intI=1; i<=n;i++)9 {TenCin>>m; One for(intj=0; j<m;j++){ ACin>>x; - S[i].insert (x); - } the } -Cin>>K; - for(intI=0; i<k;i++){ -Cin>>y1>>Y2; + intSame=0; - Set<int>:: iterator AA; + for(Aa=s[y1].begin (); Aa!=s[y1].end (); aa++){ A if(S[y2].find (*AA)! =s[y2].end ()) atsame++; - } - Doubleans=same*1.0/(S[y1].size () +s[y2].size ()-same); -printf"%.2f%%\n", ans* -); - } - return 0; in}
l2-005. Set Similarity degree