时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 钻石 Diamond
题目描述 Description

给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出现过。

输入描述 Input Description

第一行两个整数 n 和m。

第二行n个正整数(1<=n<= 100000)

第三行m个整数(1<=m<=100000)

输出描述 Output Description

一共m行,若出现则输出YES,否则输出NO

样例输入 Sample Input

4 2

2 1 3 4

1 9

样例输出 Sample Output

YES

NO

数据范围及提示 Data Size & Hint

所有数据都不超过10^8

分类标签 Tags 点此展开

哈希60分RE代码 head数组开小了

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #define mo1 12421
  5. #define mo2 34343
  6. using namespace std;
  7.  
  8. struct node
  9. {
  10. int next;
  11. int to;
  12. }e[];
  13. int n,m,i,j,tot,head[];
  14. void add(int u,int v)
  15. {
  16. tot++;
  17. e[tot].next=head[u];
  18. e[tot].to=v;
  19. head[u]=tot;
  20. }
  21. int get_hash(int k)
  22. {
  23. int h=;
  24. while(k)
  25. {
  26. h=h*+k%;
  27. k/=;
  28. }
  29. return h%mo2;
  30. }
  31. bool query(int u,int v)
  32. {
  33. for(int i=head[u];i;i=e[i].next)
  34. {
  35. if(e[i].to==v)
  36. return true;
  37. }
  38. return false;
  39. }
  40. int main()
  41. {
  42. int a;
  43. cin>>n>>m;
  44. while(n--)
  45. {
  46. cin>>a;
  47. int y=get_hash(a);
  48. add(a,y);
  49. }
  50. while(m--)
  51. {
  52. cin>>a;
  53. int y=get_hash(a);
  54. if(query(a,y))
  55. cout<<"YES"<<endl;
  56. else cout<<"NO"<<endl;
  57. }
  58. }

哈希满分做法

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #define mo1 12421
  5. #define mo2 34343
  6. using namespace std;
  7.  
  8. struct node
  9. {
  10. int next;
  11. int to;
  12. }e[];
  13. int n,m,i,j,tot,head[];
  14. void add(int u,int v)
  15. {
  16. tot++;
  17. e[tot].next=head[u];
  18. e[tot].to=v;
  19. head[u]=tot;
  20. }
  21. int get_hash(int k)
  22. {
  23. int h=;
  24. while(k)
  25. {
  26. h=h*+k%;
  27. k/=;
  28. }
  29. return h%mo2;
  30. }
  31. bool query(int u,int v)
  32. {
  33. for(int i=head[u];i;i=e[i].next)
  34. {
  35. if(e[i].to==v)
  36. return true;
  37. }
  38. return false;
  39. }
  40. int main()
  41. {
  42. int a;
  43. cin>>n>>m;
  44. while(n--)
  45. {
  46. cin>>a;
  47. int y=get_hash(a);
  48. add(a,y);
  49. }
  50. while(m--)
  51. {
  52. cin>>a;
  53. int y=get_hash(a);
  54. if(query(a,y))
  55. cout<<"YES"<<endl;
  56. else cout<<"NO"<<endl;
  57. }
  58. }

STL满分做法

  1. #include<map>
  2. #include<iostream>
  3. using namespace std;
  4. int s[];
  5. map<int,bool>g;
  6. int main()
  7. {
  8. int n,m,ss;
  9. cin>>n>>m;
  10. for(int i=;i<=n;++i)
  11. {
  12. cin>>s[i];
  13. g[s[i]]=;
  14. }
  15. for(int i=;i<=m;++i)
  16. {
  17. cin>>ss;
  18. if(g[ss]==)
  19. cout<<"YES"<<endl;
  20. else cout<<"NO"<<endl;
  21. }
  22. return ;
  23. }

Codevs 1230 STL万岁。。 。的更多相关文章

  1. Codevs 1860 最大数 string大法好,STL万岁。。

    题目描述 Description 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 输入描述 Input Description 第一行一个正整数n. 第二行n个正整数,空格隔开 ...

  2. codevs 1230 元素查找

    题目链接:http://codevs.cn/problem/1230/ 题解: 会有很多方法写这道题,写个裸的哈希练练手 #include<cstdio> ,MOD=; int n,m,h ...

  3. codevs 1230【pb_ds】

    题目链接[http://codevs.cn/problem/1230/] 题意:给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出现过. 题解:很简单的一道题,可以选择用 ...

  4. [Codevs 1230]元素查找(手写哈希表)

    题目连接:http://codevs.cn/problem/1230/ 说白了就是要我们自己手写一个哈希表的数据结构来实现加入和查找功能.map也能直接过(我第一次写就是用map骗AC的) 提一下个人 ...

  5. AC日记——元素查找 codevs 1230

    1230 元素查找  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 给出n个正整数,然后有 ...

  6. 元素查找(codevs 1230)

    1230 元素查找  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 给出n个正整数,然后有m个询问,每 ...

  7. codevs——1230 元素查找

    时间限制: 1 s 空间限制: 128000 Ks 题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 给出n个正整数,然后有m个询问,每个询问一个整数 ...

  8. STL || HDU 1894 String Compare

    如果一个词包含再另一个词的前面(前缀),是一对前缀,求一共有多少对 *解法:STL万岁 #include<string>:https://www.cnblogs.com/SZxiaochu ...

  9. 基础算法学习以及$STL$的使用

    1.优先队列 (1)大根堆(小顶堆) priority_queue<int,vector<int>,greater<int> >q; (2)小根堆(大顶堆) pri ...

随机推荐

  1. 如何退出调起多个Activity的Application?

    1.记录打开的Activity 每打开一个activity,即记录下来,需要关闭时,关闭每一个activity即可. 2.发送特定的广播 在需要结束应用时,发送一个特定广播,每个activity收到此 ...

  2. 谈谈vertical-align的text-bottom和text-top - 韦奕

    学习资料 : http://www.tuicool.com/articles/uuYvMv 友情链接  行高   替换元素,非替换元素,内联元素,块元素

  3. mysql源码解读之事务提交过程(二)

    上一篇文章我介绍了在关闭binlog的情况下,事务提交的大概流程.之所以关闭binlog,是因为开启binlog后事务提交流程会变成两阶段提交,这里的两阶段提交并不涉及分布式事务,当然mysql把它称 ...

  4. 问题解决——VC 断点 无效 一个可能情况?

    =================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载  请通过右 ...

  5. 五、Android学习第四天补充——Android的常用控件(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 五.Android学习第四天补充——Android的常用控件 熟悉常用的A ...

  6. 完整卸载 kubuntu-desktop from Ubuntu 14.04 LTS

    系统 ubuntu 14.04 LTS 64Bit 目的:卸载kubuntu-desktop 方法一: sudo apt-get remove libkde3support4 k3b-data ntr ...

  7. 嵌入式Linux开发板

    嵌入式Linux开发板开发介绍: iTOP-4412嵌入式Linux开发板搭载三星Exynos四核处理器,配备1GB内存,4GB固态硬盘EMMC存储,独家配备三星S5M8767电源管理,配备Andro ...

  8. jquery back to top 页面底部的返回顶部按钮

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. [转][前端优化]使用Combres合并对js、css文件的请求

    本文转自:http://www.cnblogs.com/parry/archive/2011/01/28/Reduce_Http_Request_Using_Combres_For_Js_Css.ht ...

  10. 荒芜的周六-PHP之面向对象(三)

    hi 又是开森的周六了.积攒的两周的衣服,终于是差不多洗完了.大下午的才来学点东西~~ 1.PHP面向对象(三) 四.OOP的高级实践 4.3 Static-静态成员 <?phpdate_def ...