codevs 1230 元素查找

题目链接:http://codevs.cn/problem/1230/

题解:

  会有很多方法写这道题,写个裸的哈希练练手

 1 #include<cstdio>
 2 const int MAXN=100000010,MOD=10000;
 3 int n,m,head[MAXN/MOD],cnt;
 4 struct edge
 5 {
 6     int v,next;
 7 }e[MAXN/10];
 8 int hash(int x)
 9 {
10     return x%MOD;
11 }
12 void add(int x,int y)
13 {
14     e[++cnt]=(edge){y,head[x]};
15     head[x]=cnt;
16 }
17 bool query(int x,int y)
18 {
19     for(int i=head[x];i;i=e[i].next)
20     {
21         if(e[i].v==y)return true;
22     }
23     return false;
24 }
25 int main()
26 {
27     scanf("%d%d",&n,&m);
28     int x;
29     for(int i=1;i<=n;++i)
30     {
31         scanf("%d",&x);
32         add(hash(x),x);
33     }
34     for(int i=1;i<=m;++i)
35     {
36         scanf("%d",&x);
37         if(query(hash(x),x))printf("YES\n");
38         else printf("NO\n");
39     }
40     return 0;
41 }

 

posted @ 2016-10-25 20:48  xqmmcqs  阅读(173)  评论(0编辑  收藏  举报