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. HDU 1978 记忆化搜索(dfs+dp)

    Y - How many ways Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  2. 《linux备份与恢复之一》.tar.bz2与.tar.gz格式的文本压缩率比较

    对于文本压缩,据说bzip的算法要优于gzip,从而拥有更好的压缩比.特地找了两个文件来做一下测试,以下为测试结果:   (1)源文件为591MB, .tar.bz2文件为61MB(10.32%), ...

  3. 如何下载google play免费应用的apk文件

    到这里: http://apps.evozi.com/apk-downloader/ 一看便知.

  4. 【Hibernate】Hibernate系列6之HQL查询

    HQL查询 6.1.概述 6.2.分页查询 6.3.命名查询 6.4.投影查询-部分字段查询 6.5.报表查询 6.6.迫切左外连接.左外连接 6.7.迫切内连接.内连接 6.8.QBC查询.本地查询

  5. Segment Tree Modify

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

  6. iOS7中都Bar的透明问题

    /* New behavior on iOS 7. Default is YES. You may force an opaque background by setting the property ...

  7. 手动构建Servlet项目的流程

    前面讨论过手动建立jsp的项目,jsp是tomcat服务器负责编译执行,所以配置相对简单,而Servlet需要先把java源文件编译成字节码class文件,然后再执行,所以需要servlet-api. ...

  8. byte[]和InputStream的相互转换[转载]

    1:byte[]转换为InputStream InputStream sbs = new ByteArrayInputStream(byte[] buf); 2:InputStream转换为Input ...

  9. 埃及分数(codevs 1288)

    题目描述 Description 在古埃及,人们使用单位分数的和(形如1/a的, a是自然数)表示一切有理数. 如:2/3=1/2+1/6,但不允许2/3=1/3+1/3,因为加数中有相同的. 对于一 ...

  10. java 小知识点

    1.转Java中Vector和ArrayList的区别   首先看这两类都实现List接口,而List接口一共有三个实现类,分别是ArrayList.Vector和LinkedList.List用于存 ...