[复习]哈希表 集合A

来源:互联网 发布:spring resource和java 编辑:程序博客网 时间:2024/05/22 12:36

题目描述
给定两个集合 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 am confused!”

输入格式
输入文件有两行,分别表示两个集合,每行的第一个整数为这个集合的元素个数(至少一个),然后紧跟着这个集合的元素(均为不同的正整数)。

输出格式
输出文件只有一行,就是 A、B 的关系。

样例数据1
输入

2 55 27
2 55 27

输出

A equals B

样例数据2
输入

3 9 24 1995
2 9 24

输出

B is a proper subset of A

样例数据3
输入

3 1 2 3
4 1 2 3 4

输出

A is a proper subset of B

样例数据4
输入

3 1 2 3
3 4 5 6

输出

A and B are disjoint

样例数据5
输入

2 1 2
2 2 3

输出

I am confused!

分析:哈希表模板

代码

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<ctime>#include<cmath>#include<algorithm>#include<cctype>#include<iomanip>#include<queue>#include<set>using namespace std;int getint(){    int sum=0,f=1;    char ch;    for(ch=getchar();!isdigit(ch)&&ch!='-';ch=getchar());    if(ch=='-')    {        f=-1;        ch=getchar();    }    for(;isdigit(ch);ch=getchar())        sum=(sum<<3)+(sum<<1)+ch-48;    return sum*f;}const int maxx=111317;//取一个大质数(很有可能取得不够“优秀”,这是我尝试的第7个质数,其他的都要T点)int a,b,s,j,x,index[211317];//哈希表只需要大质数加上有多少数那么大的数组就可以了int locate(int t){    int tmp;    tmp=t%maxx;    while((index[tmp]!=0)&&index[tmp]!=t)        tmp++;    return tmp;}bool find(int t){    if(index[locate(t)]==t)        return true;    else        return false;} int main(){    freopen("A.in","r",stdin);    freopen("A.out","w",stdout);    a=getint();    for(int i=1;i<=a;++i)    {        x=getint();        index[locate(x)]=x;    }    b=getint();    s=0;    for(int i=1;i<=b;++i)    {        x=getint();        if(find(x)==false) s++;     }    if(a==b && s==0)        cout<<"A equals B"<<'\n';    else if(a>b && s==0)        cout<<"B is a proper subset of A"<<'\n';    else if(b>a && s==b-a)        cout<<"A is a proper subset of B"<<'\n';    else if(s==a || s==b)        cout<<"A and B are disjoint"<<'\n';    else        cout<<"I am confused!"<<'\n';    return 0;} 

本题结。

原创粉丝点击