Similar number set (application of set)

青藤210764 Similar sets of numbers

Idea: The first one is the common number of the two number sets, the second is all the numbers of the two number sets, both of which cannot have duplicate numbers, then divide it by *100 and add a %, create a set array, and then Insert it, and then find the array number set corresponding to x and y when inputting, traverse all the elements of x, and then count the number set of y, the first is the value of the counter, and the second is the length of the two number sets and- Repeat the part (that is, the value of the counter), and then output in turn, of course, you can also combine the two numbers (because the collection can be deduplicated), and then so on, but it will time out

Code:
1.100 points

#include<bits/stdc++.h>
using namespace std;
set<int>a[51];
int n,m,i,j,x,y,lx,ly,cnt;
int main(){
    
    
	ios::sync_with_stdio(false);
	cin>>n;
	for(i=1;i<=n;i++){
    
    
		cin>>m;
		for(j=0;j<m;j++){
    
    
			cin>>x;
			a[i].insert(x);
		}
	}
	cin>>m;
	while(m--){
    
    
		cin>>x>>y;
		lx=a[x].size();
		ly=a[y].size();
		cnt=0;
		for(set<int>::iterator it=a[x].begin();it!=a[x].end();++it)
			if(a[y].find(*it)!=a[y].end())
				cnt++;
		printf("%.2lf%%\n",cnt*100.0/(lx+ly-cnt));
	}
    return 0;
}

2.40 minutes

#include<bits/stdc++.h>
using namespace std;
set<int>a[51],b;
int n,m,i,j,k,x,y,lx,ly,lb;
int main(){
    
    
	ios::sync_with_stdio(false);
	cin>>n;
	for(i=1;i<=n;i++){
    
    
		cin>>m;
		for(j=0;j<m;j++){
    
    
			cin>>x;
			a[i].insert(x);
		}
	}
	cin>>k;
	while(k--){
    
    
		cin>>x>>y;
		lx=a[x].size();
		ly=a[y].size();
		set<int>::iterator it=a[x].begin();
		for(;it!=a[x].end();++it)b.insert(*it);
	    for(it=a[y].begin();it!=a[y].end();++it)b.insert(*it);
		lb=b.size();
		printf("%.2lf%%\n",(lx+ly-lb)*1.0/lb*100);
		b.clear();
	}
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_52536621/article/details/113747602
set
set