codevs 1230 元素查找(hash)
来源:互联网 发布:手机短信软件哪个好 编辑:程序博客网 时间:2024/06/08 12:19
题目描述 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思路:给定一个字符串(或数字串)a,模一个数(一般为质数),令x=a%mod,以x为起点,a为终点建立一条边,查询时扫描某一个点的连边即可。
#include<iostream>using namespace std;const int maxn=500010;int n,m,tot,tmp,head[maxn];struct node{ int to; int next;}e[maxn];int get_hash(int x){ return x%10007;}void add_edge(int u,int v){ tot++; e[tot].to=v; e[tot].next=head[u]; head[u]=tot;}bool find(int u,int v){ for(int i=head[u];i;i=e[i].next) if(e[i].to==v) return 1; return 0;}int main(){ int x,y; cin>>n>>m; for(int i=1;i<=n;i++) { cin>>tmp; x=get_hash(tmp); add_edge(x,tmp); } for(int i=1;i<=m;i++) { cin>>tmp; x=get_hash(tmp); if(find(x,tmp)) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0;}
6 0
- codevs 1230 元素查找(hash)
- codevs 1230 元素查找(hash)
- CODEVS 1230 元素查找
- CODEVS 1230元素查找
- CODEVS 1230 元素查找
- 【codevs 1230】元素查找
- CODEVS 1230 元素查找
- CODEVS 1230 元素查找
- 【CODEVS 1230】元素查找 哈希表
- codevs 1230 元素查找 二分
- [Codevs 1230]元素查找(手写哈希表)
- 【codevs 1230】元素查找 splay……
- codevs 1230_元素查找_hash
- 元素查找(hash初步)
- Hash查找和为x的两个元素
- Hash查找
- hash查找
- hash查找
- 安生与七月
- 创建线程的几种方式,以及为什么启动线程不用run,而用start方法!!!
- UI设计学习--PPI、DPI、PX、DP
- java bean入门
- 6. Gobject 学习总结
- codevs 1230 元素查找(hash)
- NOIP2010 引水入城
- ERROR 1130 MySql
- Android之Json解析方式和解析框架
- 比特币区块链代码分析
- servlet的config对象
- Python函数作用域和闭包
- Java中的Copy-On-Write容器
- 关于素数的线性筛法