[复习]哈希表 集合B

题目描述
给定两个集合 A、B,集合内的任一元素 x 满足 1≤x≤ 109 ,并且每个集合的元素个数不大于 107 。我们希望求出 A、B 之间的关系,只需确定在 B 中但是不在 A 中的元素的个数即可。

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

输出格式
输出文件只有一个整数,即在 B 中但是不在 A 中的元素的个数即可。

样例数据1
输入

2 55 27
2 55 27

输出

0

样例数据2
输入

3 9 24 1995
2 9 24

输出

0

样例数据3
输入

3 1 2 3
4 1 2 3 4

输出

1

样例数据4
输入

3 1 2 3
3 4 5 6

输出

3

样例数据5
输入

2 1 2
2 2 3

输出

1

分析:哈希表模板,注意事项见集合A

代码

#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;
}

int a,b,x,s,maxx=111317;
int index[10111317];

int locate(int t)
{
    int 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("B.in","r",stdin);
    freopen("B.out","w",stdout);

    a=getint();
    for(int i=1;i<=a;++i)
    {
        x=getint();
        index[locate(x)]=x;
    }

    b=getint();
    for(int i=1;i<=b;++i)
    {
        x=getint();
        if(!find(x))
            s++;
    }

    cout<<s<<'\n';
    return 0;
}

本题结。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值