codevs 1230 元素查找(hash)
来源:互联网 发布:淘宝在哪里改邮编 编辑:程序博客网 时间:2024/05/28 04:56
题目描述 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
题解:这道题可以用hash表来做,对于每个数我们mod一下10000作为他的hash值,如果存在冲突就用链表存一下。查找的时候直接根据要查找数的hash值就能直接找到这个数有没有出现过。
PS:这道题我刚开始mod10007然后WA了,我改成mod10000就AC了,不是说要尽量mod素数吗QAQ
代码如下:
#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int MAXN=100000010;const int mod=10000;int tot,head[MAXN/mod];struct links{ int f,t;}es[MAXN/10];int hash(int x){ return x%mod;//以他mod10000作为hash值 }void add(int x,int y){ es[++tot]=(links){y,head[x]};//按照他的hash值存入链表 head[x]=tot;}bool ask(int x,int y){ for(int i=head[x];i;i=es[i].t) if(es[i].f==y) return true;//在链表中找到这个数 return false;}int main(){ int n,m,x; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&x); add(hash(x),x);//按照他的hash值加入链表 } for(int i=1;i<=m;i++) { scanf("%d",&x); if(ask(hash(x),x)) printf("YES\n");//通过他的hash值能够找到这个数,说明出现过 else printf("NO\n"); } return 0;}
5 0
- codevs 1230 元素查找(hash)
- codevs 1230 元素查找(hash)
- CODEVS 1230 元素查找
- CODEVS 1230元素查找
- CODEVS 1230 元素查找
- 【codevs 1230】元素查找
- CODEVS 1230 元素查找
- CODEVS 1230 元素查找
- 【CODEVS 1230】元素查找 哈希表
- codevs 1230 元素查找 二分
- 元素查找(hash初步)
- [Codevs 1230]元素查找(手写哈希表)
- 【codevs 1230】元素查找 splay……
- codevs 1230_元素查找_hash
- Hash(哈希)查找
- Hash查找和为x的两个元素
- HASH表(高效查找)
- Hash查找
- 8大排序算法
- iOS - 默认NavigationBar
- 洛谷 P1128 [HNOI2001] 求正整数
- 勾股定理一日一证连载156
- centos7.2环境搭建
- codevs 1230 元素查找(hash)
- eclipse和oracle链接时遇到的问题
- WEB项目在运行时报错The serv…
- sublime安装及基本使用
- 曲线渐开线方程
- 为促进OCP创新,微软开源不完整的Azure服务器
- CVPR 2016-11-8
- 数字三角形升级版(棋盘型动态规划)
- Linux "ls -l"文件列表权限详解