zoukankan      html  css  js  c++  java
  • 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。

  • 相关阅读:
    浙大PAT CCCC L3-001 凑零钱 ( 0/1背包 && 路径记录 )
    二分图匹配
    Codeforces 939E Maximize ( 三分 || 二分 )
    冲刺第二周第七天
    冲刺第二周第六天
    冲刺第二周第五天
    构建之法阅读笔记04
    冲刺第二周第四天
    构建之法阅读笔记03
    构建之法阅读笔记02
  • 原文地址:https://www.cnblogs.com/OYzx/p/5627101.html
Copyright © 2011-2022 走看看