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. asp.net动态输出透明gif图片

    要使用asp.net动态输出透明gif图片,也就是用Response.ContentType = "image/GIF". 查了国内几个中文资料都没解决,最后是在一个英文博客上找到 ...

  2. call_user_func_array使用原型

    If you need to call object and class methods in PHP < 4.0.4, the following code ought to do the t ...

  3. Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  4. selec2 clone不起作用。

    <table class="table table-bordered"> <thead> <tr> <th width ="16 ...

  5. 如何使用box2d做碰撞检测

    cocos2dx3.0+vs2012编译通过. 主要是通过body->SetTransform来设置body的位置和角度,然后自己写个ContactListener来监听碰撞事件 源代码下载 # ...

  6. Codebook model 视频抠像 xp sp3 + vs2005 + OpenCV 2.3.1

    Codebook model 视频抠像 xp sp3 + vs2005 + OpenCV 2.3.1 源码及详细文档下载 svn checkout http://cvg02.googlecode.co ...

  7. Auguse 2nd, Week 32nd Tuesday, 2016

    Love me little and love me long.不求情意绵绵,但愿天长地久. Friends are relatives you make for yourself.朋友是你自己结交的 ...

  8. 6个朋友(codevs 2832)

    2832 6个朋友  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 有这么一种说法:认识6个人,你就认识全世 ...

  9. 小吃(codevs 3231)

    3231 小吃  时间限制: 1 s  空间限制: 16000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 这里简直是吃货的天堂,小吃太多了. ...

  10. 如何关闭ie9烦人的提示信息?

    ①Q:如何关闭“IE 限制活动内容”的提示? A:去掉“IE 限制活动内容”的提示: 1. 找到IE的“Internet选项”: 2. 选择“高级”选项卡: 3. 在“设置”下,找到子标题“安全”,将 ...