先介绍<algorithm>头文件中与集合运算有关的4个函数:
- set_union
-
Union of two sorted ranges
(并集:A∪B)
- set_intersection
-
Intersection of two sorted ranges
(交集:A∩B)
- set_difference
-
Difference of two sorted ranges
(差集:A-B)
- set_symmetric_difference
-
Symmetric difference of two sorted ranges
(对称差集:A△B)
很明显,这里要判断两个集合的关系,使用交集来判断就行了。
使用方法:
set_intersection(a, a + n, b, b + m, v.begin());
这里v是动态数组,用于保存交运算结果。(其他函数的使用方法均与之类似)
该函数的返回值减去v.begin()就是集合的大小。
完整代码:
/*0.016s*/ #include<bits/stdc++.h>
using namespace std; int a[100], b[100];
vector<int> v(100);///初始化空间是必须的 int main()
{
int n, m, cnt, diff;
char ch;
while (~scanf("%d%c", &a[0], &ch))
{
for (n = 1; ch != 10; ++n)
scanf("%d%c", &a[n], &ch);
cnt = scanf("%d%c", &b[0], &ch);
for (m = 1; ch != 10 && cnt == 2; ++m)
cnt = scanf("%d%c", &b[m], &ch);
sort(a, a + n);
sort(b, b + m);
diff = set_intersection(a, a + n, b, b + m, v.begin()) - v.begin();
if (diff == min(n, m))
{
if (n < m) puts("A is a proper subset of B");
else if (n > m) puts("B is a proper subset of A");
else puts("A equals B");
}
else if (diff) puts("I'm confused!");
else puts("A and B are disjoint");
}
return 0;
}