NOJ1184 迷失的邮票 散列表

来源:互联网 发布:美工平面设计培训 编辑:程序博客网 时间:2024/06/05 11:56

题意

一共收集了N张邮票,现在丢了2张,剩下N-2张…..原先收集的邮票全部是成对收集的,所以找到哪两种邮票是成单的,输出它们。(确定丢失的邮票不是同一种)

思路

因为编号比较大,可以用hash表压缩成数组可以开的下的大小。压缩直接取模就好。如果冲突就往下一个找。

代码

#include <cstdio>#include <cstring>#define MOD 1000007const int maxn = 1000010;struct node {    int cnt;    int num;};node s[maxn];int main(){    int n;    scanf("%d",&n);    for(int i = 0 ; i < n-2 ; i ++) {        int a;        scanf("%d",&a);//????        int ahash = a%MOD;        while(s[ahash].num != a && s[ahash].num != 0) {            ahash ++;            ahash = ahash%maxn;        }        s[ahash].num = a;        s[ahash].cnt ++;    }    bool first = true;    for(int i = 0 ; i < maxn ; i ++) {        if(s[i].cnt%2) {            if(first) {                first = false;                printf("%d",s[i].num);            }else printf(" %d\n",s[i].num);        }    }    return 0;}
1 0