PAT-A-1063. Set Similarity
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.
如何最快地统计两个set 中重复的元素,
一、
asize = aset.size();
bsize = bset.size();
将setb 中的元素加入到seta
totalsize = aset.size();
samesize = asize + bsize - totalsize;
有一个缺点就是如此之后,a 变样了。
二、
for (set<int>::iterator it = a.begin(), it != a.end(), ++it) {
if (b.find(*it) != b.end()) {
++samesize;
}
}
时间复杂度
实践证明,在修改方法一,在a 的copy 中添加b 的元素后,方法一在时间复杂度空间复杂度上都不如方法二。