http://wikioi.com/problem/1230/

这题我真的不好意思写题解了。。。set练手。。

#include <cstdio>
#include <set>
using namespace std;
int main() {
int n, m, t;
scanf("%d%d", &n, &m);
set<int> s;
for(int i=1; i<=n; ++i) scanf("%d", &t), s.insert(t);
for(int i=1; i<=m; ++i) scanf("%d", &t), s.count(t)?puts("YES"):puts("NO");
return 0;
}

还是写第二种方法吧,hash。(速度比第一种快)

(我从来没写过啊囧,但是一看就会了,太简单了。。)

我们一般是用mod来做hash的,但是有时会有冲突,肿莫办!!!!!

哈哈,很简单,如果有冲突我就往后跑,往后占!!!查找也是一样,不等于就往后跑,往后占!!

这样,hash的空间就可以压很小了。(但是注意,如果范围很密集,这样做会失效。。。)

#include <cstdio>
#include <set>
using namespace std;
int a[100009], md=100007;
void ins(const int &x) { for(int k=x%md; a[k]!=x; k=(k+1)%md) if(!a[k]) { a[k]=x; return; } }
int sea(const int &x) { for(int k=x%md; a[k]!=x; k=(k+1)%md) if(!a[k]) return 0; return 1; }
int main() {
int n, m, t;
scanf("%d%d", &n, &m);
set<int> s;
for(int i=1; i<=n; ++i) scanf("%d", &t), ins(t);
for(int i=1; i<=m; ++i) scanf("%d", &t), sea(t)?puts("YES"):puts("NO");
return 0;
}

题目描述 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

【wikioi】1230 元素查找(巨水题+set/hash)的更多相关文章

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

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

  2. 【BZOJ】2761: [JLOI2011]不重复数字(set+巨水题+超坑题)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2761 太水了,不说了. 但是这格式错误我已经没话说了....行末不能有空格 #include < ...

  3. codevs 1230 元素查找

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

  4. CODE[VS] 1230 元素查找

    1.题目戳这里 2.代码: #include<iostream> #include<algorithm> using namespace std; int n,m,a[1000 ...

  5. 二分--LIGHTOJ 1088查找区间(水题)

    #include <iostream> #include <cstdio> #include <cmath> using namespace std; const ...

  6. codevs——1230 元素查找

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

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

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

  8. 元素查找(codevs 1230)

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

  9. codevs1230元素查找(hash)

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

随机推荐

  1. 【OpenStack】OpenStack系列11之namaspace&openvswitch原理实践

    Namespace实现网络隔离与互通 新建ns: ip netns add foo 查看ns: ip netns 查看ns详细配置: ip netns exec foo ip addr 设置ns内部l ...

  2. Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  3. 动态设置Div坐标

    <style type="text/css"> #main{text-align:center; background-color:#9FF; height:600px ...

  4. 12.从上往下遍历二元树[LevelOrderOfBinaryTree]

    [题目] 输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印. 例如输入 8    /  \   6    10  /\     /\ 5  7   9  11 输出8    ...

  5. js判空

    2014年9月3日 11:36:10 转载的: http://blog.sina.com.cn/s/blog_755168af0100vsik.html typeof用法 typeof的运算数未定义, ...

  6. PHP exit() 输出

    2014年8月6日 10:41:00 exit($a); 当$a是bool类型,整形的时候,浏览器里是看不到任何输出的 当$a是字符串的时候浏览器里是可以看到输出的 $a = 1; exit($a); ...

  7. Windows远程桌面连接如何直接使用剪贴板功能

    连接到Windows Server服务器时,通常使用远程桌面连接,那么如果有些时候按照习惯复制本地文件到远程服务器发现无法粘贴怎么办,这个时候稍微设置一下就OK了, 首先重新运行远程桌面连接,在登陆界 ...

  8. SQL 删除存在于A表但是不存在B表中的记录

    目的是是的A表和B表某一个列集合相等 delete from A where tagetColumn not in ( select targetColumn from B)

  9. CSS鼠标样式整理

    鼠标样式的标签: cursor:*;  //该属性定义了鼠标指针放在一个元素边界范围内时所用的光标形状: 鼠标样式: 值 描述 url 需使用的自定义光标的 URL. 注释:请在此列表的末端始终定义一 ...

  10. Linux下可以替代windows的软件汇总:(不断完善中)

    http://www.ubuntukylin.com/ukylin/forum.php?mod=viewthread&tid=9530 原则:不求全面,只求实用.主要针对桌面级应用.网购:  ...