Description
任意给定两个包含1-30000个元素的集合A,B(集合中元素类型为任意整型数,且严格递增排列),求A交B、A并B、A-B和B-A集合。
Input
输入第一行为测试数据组数。每组测试数据两行,分别为集合A、B。每行第一个数n(1<=n<=30000)为元素数量,后面有n个严格递增的绝对值小于2^31代表集合中包含的数。
Output
对每组测试数据输出5行,第1行为数据组数,后4行分别为按升序输出两个集合的A交B、A并B、A-B和B-A集合。格式见样例。
Sample Input
1
3 1 2 5
4 2 3 5 8
Sample Output
Case #1:
2 5
1 2 3 5 8
1
3 8
HINT
考察知识点:有序表合并,时间复杂度O(n),空间复杂度O(n)
Append Code
析:这个题用set模拟就好,反正set都有这些函数。也可以自己写。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #define ALL(x) x.begin(),x.end() #define INS(x) inserter(x,x.begin()) #define frer freopen("in.txt", "r", stdin) #define frew freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 1e5 + 5; const int mod = 1e9 + 7; const char *mark = "+-*"; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline int Min(int a, int b){ return a < b ? a : b; } inline int Max(int a, int b){ return a > b ? a : b; } inline LL Min(LL a, LL b){ return a < b ? a : b; } inline LL Max(LL a, LL b){ return a > b ? a : b; } inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } set<int> A, B, C; set<int> :: iterator it; void print(set<int> &C){ for(it = C.begin(); it != C.end(); ++it){ if(it == C.begin()) printf("%d", *it); else printf(" %d", *it); } printf("\n"); } int main(){ int T; cin >> T; for(int kase = 1; kase <= T; ++kase){ printf("Case #%d:\n", kase); A.clear(); B.clear(); scanf("%d", &n); for(int i = 0; i < n; ++i){ scanf("%d", &m); A.insert(m); } scanf("%d", &n); for(int i = 0; i < n; ++i){ scanf("%d", &m); B.insert(m); } C.clear(); set_intersection(ALL(A), ALL(B), INS(C)); print(C); C.clear(); set_union(ALL(A), ALL(B), INS(C)); print(C); C.clear(); set_difference(ALL(A), ALL(B), INS(C)); print(C); C.clear(); set_difference(ALL(B), ALL(A), INS(C)); print(C); } return 0; }