The topics are as follows:
Given-sets of integers, the similarity of the sets is defined to being Nc/nt*100%, where n C is the number of Distinct common numbers shared by the same sets, and N T is the total number ofdistinct numbers in the. Your job is to calculate the similarity of any given pair of sets.
Input Specification:
each input file contains one test case. Each case first gives a positive integer N (<=50) which are the total number of sets. Then N lines follow, each gives a set with a positive M (<=104) and followed by M integers i n the range [0, 109]. After the input of sets, a positive an integer K (<=2000) is given, followed by K lines of queries. Each query gives a pair of set numbers (the sets is numbered from 1 to N). All the numbers-a line is separated by a space.
Output Specification:
For each query, print on one line the similarity of the sets, in the percentage form accurate up to 1 decimal place.
Sample Input:
33 99 87 1014 87 101 5 877 99 101 18 5 135 18 9921 21 3
Sample Output:
50.0%33.3%
This topic requires that the common part NC and all non-repeating elements nt be found from both sets, and then the ratio of NC to NT is computed.
If you use multiple maps to query, the topic is easy to complete, but this problem for the misuse of map restrictions, I initially use multiple maps, find the way to find common parts and all non-repeating elements when the last case appeared a time-out, and then changed a thought.
Only one map is used to filter the elements that are duplicated during a collection input, so that each collection element is different, and then the elements of each collection are sorted in ascending order, and the comparison can be made in different maps, using two pointers cura, Curb, respectively, to represent the traversed set a, The position of collection B.
To initialize NC = Nt = 0, follow these steps:
Cur's Moving principle: who moves small and who goes with the big close.
① Check if Cura and curb are out of bounds and jump to ③.
② if Seta[cura] > Setb[curb], at this time should move CurB, according to the movement principle, in the CurB movement process, all encountered Setb[curb] elements are seta, at this time nt++; if seta[cura] = = SETB[CURB], indicating that a common element was encountered, nc++,nt++ (note that common elements are also counted as one of the different elements), and Cura and CurB are moved; if Seta < Setb[curb], then Cura should be moved, according to the principle of movement, All encountered Seta[cura] elements are not SETB, at this time nt++.
③ check whether Seta and Setb have not traversed, if there is, that the rest should belong to NT.
The code is as follows:
#include <iostream> #include <map> #include <stdio.h> #include <vector> #include <algorithm >using namespace Std;int main () {int nc,nt; Map<int,int> Commonmap; int N,m,ele; Cin >> N; vector<vector<int> > Sets (n+1); for (int i = 1; I <= N; i++) {scanf ("%d", &m); Commonmap.clear (); for (int j = 0; J < M; J + +) {scanf ("%d", &ele); if (Commonmap.find (ele) = = Commonmap.end ()) {Commonmap[ele] = 1; Sets[i].push_back (ele); }} sort (Sets[i].begin (), Sets[i].end ()); } int k,a,b; Cin >> K; for (int i = 0; i < K; i++) {scanf ("%d%d", &a,&b); Vector<int> SetA = Sets[a]; Vector<int> SETB = sets[b]; Nc = Nt = 0; int CurA = 0; int CurB = 0; while (CurA < Seta.size () && CurB < Setb.size ()) {if (Seta[cura] < setb[curb]) { nt++; cura++; }else if (Seta[cura] > Setb[curb]) {nt++; curb++; }else{nc++; nt++; cura++; curb++; }} if (CurA < Seta.size ()) Nt + = Seta.size ()-CurA; if (CurB < Setb.size ()) Nt + = Setb.size ()-CurB; printf ("%0.1f%%\n", (float) nc/nt * 100); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
1063. Set Similarity (25)