PAT 1063. Set Similarity

1063. Set Similarity

题目大意

给定 n 个集合, k 个询问, 求任意两个集合的并集和合集.

思路

一道裸的考察 STL 中 set 的题, 我居然还用 hash 错过一遍, 用 vector勉强过了, 最后才发现原来如此简单.

代码

#include <cstdio>
#include <set>
#include <vector>
using namespace std;
int main(){
    int nSet;
    scanf("%d", &nSet);
    vector<set<int> > sts(nSet + 1);
    for(int i = 1; i <= nSet; i++){
        int nNum;
        scanf("%d", &nNum);
        set<int> temp;
        for(int j = 0; j < nNum; j++){
            int val;
            scanf("%d", &val);
            temp.insert(val);
        }
        sts[i] = temp;
    }
    int nQuery;
    scanf("%d", &nQuery);
    for(int i = 0; i < nQuery; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        int Nc = 0;
        int Nt = sts[a].size() + sts[b].size();
        for(set<int>::iterator ita = sts[a].begin(); ita != sts[a].end(); ++ita){
            if(sts[b].find(*ita) != sts[b].end()){
                Nc++; Nt--;
            }
        }
        printf("%.1f%%
", Nc * 100.0 / Nt);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/1pha/p/7799756.html