Description
给定两个集合A、B,集合内的任一元素x满足1 ≤ x ≤ 109,并且每个集合的元素个数不大于105。我们希望求出A、B之间的关系。
任 务 :给定两个集合的描述,判断它们满足下列关系的哪一种:
A是B的一个真子集,输出“A is a proper subset of B”
B是A的一个真子集,输出“B is a proper subset of A”
A和B是同一个集合,输出“A equals B”
A和B的交集为空,输出“A and B are disjoint”
上述情况都不是,输出“I’m confused!”
Input
输入有两行,分别表示两个集合,每行的第一个整数为这个集合的元素个数(至少一个),然后紧跟着这个集合的元素(均为不同的正整数)
Output
只有一行,就是A、B的关系。
Sample Input
样例1
2 55 27
2 55 27
样例2
3 9 24 1995
2 9 24
样例3
3 1 2 3
4 1 2 3 4
样例4
3 1 2 3
3 4 5 6
样例5
2 1 2
2 2 3
Sample Output
样例1
A equals B
样例2
B is a proper subset of A
样例3
A is a proper subset of B
样例4
A and B are disjoint
样例5
I'm confused!
解题思路
HASH模板
将
a
i
a_i
ai存入HASH
查找
b
i
b_i
bi是否存在于HASH
#include<iostream>
#include<cstdio>
using namespace std;
const int F=149993;
int a[F],n,m,x,ans;
int mod(int x){
return (x%F);
}
int into(int x){
int cnt=mod(x),i=0;
while(i<F&&a[mod(cnt+i)]&&a[mod(cnt+i)]!=x)
i++;
return (mod(cnt+i));
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&x);
a[into(x)]=x;
}
scanf("%d",&m);
for(int i=1;i<=m;i++){
scanf("%d",&x);
if(a[into(x)]==x)ans++;
}
if (ans==n&&n==m) printf("A equals B");
else if (ans==m) printf("B is a proper subset of A");
else if (ans==n) printf("A is a proper subset of B");
else if (!ans) printf("A and B are disjoint");
else printf("I'm confused!");
}