集合 哈希表

问题描述

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

输入文件

第一行两个数m和n分别表示集合A和集合B元素的个数。 以下两个分别是集合A和集合B的元素。

输出文件

一个数,表示在B中但是不在 A 中的元素的个数。

输入样例

5 6
1 3 8 4 9
4 8 9 10 12 13 

输出样例

3

限制和约定

时间限制:1s

空间限制:128MB

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int a[10001],b,m,n,ans(0),first[1000010],p=1000007,next[10001];
bool hash(int x)
{
    int k=x%p,pi(0);
    for(int i=first[k];i>0;i=next[i])
    if(a[i]==x) pi=1;
    if(pi==0) return true;
    return false;
}
int main()
{
    cin>>m>>n;
    for(int i=1;i<=m;i++)
    {
    cin>>a[i];
    next[i]=first[a[i]%p];
    first[a[i]%p]=i;
    }
    for(int j=1;j<=n;j++)
    {
    cin>>b;
    if(hash(b)) ans++;
    }
    cout<<ans;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hfang/p/11240013.html