日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1063. Set Similarity (25)

發布時間:2025/6/17 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1063. Set Similarity (25) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目例如以下:

Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc?is the number of distinct common numbers shared by the two sets, and Nt?is the total number of distinct numbers in the two sets. 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 is the total number of sets. Then N lines follow, each gives a set with a positive M (<=104) and followed by M integers in the range [0, 109]. After the input of sets, a positive integer K (<=2000) is given, followed by K lines of queries. Each query gives a pair of set numbers (the sets are numbered from 1 to N). All the numbers in a line are separated by a space.

Output Specification:

For each query, print in one line the similarity of the sets, in the percentage form accurate up to 1 decimal place.

Sample Input: 3 3 99 87 101 4 87 101 5 87 7 99 101 18 5 135 18 99 2 1 2 1 3 Sample Output: 50.0% 33.3%

這道題目要求從兩個集合中不反復的找到公共部分Nc和全部不反復元素Nt,然后計算Nc占Nt的比例。

假設使用多個map進行查詢,題目非常easy完畢,可是這道題對于濫用map進行了限制。我最初使用多個map。查找方式找出公共部分和所有不反復元素時最后一個case出現了超時,后來換了一個思路。

僅僅使用一個map,用于過濾在一個集合輸入過程中反復的元素,這樣得到的每一個集合元素都是不同的,然后把每一個集合元素依照升序排序。最后比較時就能夠不同map了。我們使用兩個指針curA、curB分別代表遍歷到的集合A、集合B的位置。

初始化Nc = Nt = 0,依照以下的操作進行:

cur的移動原則:誰小移動誰去跟大的接近。

①檢查curA和curB是否越界,越界則跳到③。

假設setA[curA] > setB[curB],此時應該移動curB,依據移動原則。在curB移動過程中。全部遇到的setB[curB]元素均為setA中所沒有的,此時Nt++;假設setA[curA] == setB[curB],說明碰到了公共元素,此時Nc++。Nt++(注意公共元素也算作不同元素中的一個),而且curA和curB均移動;假設setA < setB[curB],此時應該移動curA。依據移動原則,全部遇到的setA[curA]元素均為setB所沒有的,此時Nt++。

③檢查setA和setB是否有沒遍歷完的,假設有,說明剩下的都應該屬于Nt。

代碼例如以下:

#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; }

轉載于:https://www.cnblogs.com/wzzkaifa/p/7077693.html

總結

以上是生活随笔為你收集整理的1063. Set Similarity (25)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。