简单的一个哈希查找函数

来源:互联网 发布:java爬虫书籍推荐 编辑:程序博客网 时间:2024/06/17 20:44
代码如下:
#include <iostream>#include <string.h>#include <stdio.h>#include <set>#define m 100000typedef long long LL;int h[1000020],p[1000020];LL a[1000020];int Search( LL key ){   int x=key%m;   int next = h[x];   while(next)   {       if( a[next] == key ) return 1;       next = p[next];   }   return 0;}int main(){    LL n, t;    scanf ( "%lld %lld", &n, &t );    memset(h,0,sizeof(h));    memset(p,0,sizeof(p));    for( LL i = 1; i <= n; i ++ )    {        scanf ( "%lld", &a[i] );        int hi = a[i]%m;        p[i]=h[hi];        h[hi]=i;    }    for( LL i = 0; i < t; i ++ )    {        LL y;        scanf ( "%lld",&y);        if(Search(y)) printf("yes\n");        else printf("no\n");    }    return 0;}

0 0
原创粉丝点击