[复习]哈希表 集合A

题目描述
给定两个集合 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;
} 

本题结。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值