Hash_集合

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 #define maxn 100005
 7 #define maxm 2000000
 8 int na,nb,now[maxm],prep[maxn],val[maxn];
 9 void read(int &x){
10     x=0; int f=1; char ch;
11     for (ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') f=-1;
12     for (;isdigit(ch);ch=getchar()) x=x*10+ch-'0'; x*=f;
13 }
14 void Ha(int x){
15     int pos=val[x]%maxm+1;
16     prep[x]=now[pos],now[pos]=x;
17 }
18 bool search(int x){
19     int pos=x%maxm+1; bool bo=0;
20     for (int i=now[pos];i;i=prep[i]){
21         if (val[i]==x){
22             bo=1; break;
23         }
24     }
25     return bo;
26 }
27 int main(){
28     memset(now,0,sizeof(now));
29     read(na);
30     for (int i=1;i<=na;i++) read(val[i]),Ha(i);
31     int ans=0;
32     read(nb); int x;
33     for (int i=1;i<=nb;i++){
34         read(x);
35         if (!search(x)) ans++;
36     } printf("%d
",ans);
37     return 0;
38 }
View Code

题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1720

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

做法:Hash入门题,我们对第一个数组hash,存入哈希表,第二个数组在Hash数组上查询即可,统计答案。

Hash。

原文地址:https://www.cnblogs.com/OYzx/p/5627101.html