AcWing 3688:集合交并 ← STL set(复旦大学考研机试题)
STL set 常用函数解析:https://blog.csdn.net/hnjzsyjyj/article/details/127017796
【题目来源】
https://www.acwing.com/problem/content/3691/
【题目描述】
输入两个集合,分别求其交集和并集中元素的个数,每个集合中可能存在相同的元素,而最终的交集和并集中应该不存在。
【输入格式】
第一行输入两个整数 n,m 表示两个集合中元素的个数。
第二行输入 n 个整数,表示第一个集合中的元素。
第三行输入 m 个整数,表示第二个集合中的元素。
【输出格式】
输出两个整数以空格分开,表示其交集和并集中元素的个数。
【输入样例】
4 5
3 4 7 3
4 6 3 2 6
【输出样例】
2 5
【数据范围】
1≤n,m≤10^5,
给定集合元素取值范围 [1, 10^9]。
【算法分析】
STL set 常用函数解析:https://blog.csdn.net/hnjzsyjyj/article/details/127017796
【算法代码】
#include <bits/stdc++.h>
using namespace std;
unordered_set<int> a,b,u;
int n,m,x;
int cnt;
int main() {
cin>>n>>m;
while(n--) {
cin>>x;
a.insert(x),u.insert(x);
}
while(m--) {
cin>>x;
b.insert(x),u.insert(x);
}
for(auto x:a) { //Intersection
if(b.count(x)) cnt++;
}
cout<<cnt<<" "<<u.size();
return 0;
}
/*
in:
4 5
3 4 7 3
4 6 3 2 6
out:
2 5
*/
【参考文献】
https://www.acwing.com/solution/content/287835/
https://blog.csdn.net/hnjzsyjyj/article/details/158689757
https://blog.csdn.net/hnjzsyjyj/article/details/127017796
https://blog.csdn.net/hnjzsyjyj/article/details/125118597
更多推荐

所有评论(0)