[SDUT](2123)查找练习 hash——出现过的数字 ---哈希查找(查找)
查找练习 hash——出现过的数字
Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
有一个数据字典,里面存有n个数字(n<=100000),小明现在接到一个任务,这项任务看起来非常简单——给定m个数字,分别查询这m个数字是否出现在字典之中;但是考虑到数据量的问题,小明找到了善于编程的你,希望你可以帮他解决这个问题。
Input
输入数据只有一组!
第一行包含两个整数n m,分别代表字典中数字的个数和要查询的数字的个数。
接着n行代表字典中的n个数字。
最后m表示要查询的数字。
Output
如果某个数字存在,则输出YES,否则输出NO
Example Input
5 3 1 2 3 4 5 5 4 10
Example Output
YES YES NO
Hint
解题新知:Hash函数f(x)=x,最简单的Hash表
AC代码:
#include<iostream> #include<cstring> using namespace std; int Hash[100005]; int main() { int n,m; int num; cin>>n>>m; memset(Hash,-1,sizeof(Hash)); while(n--) { cin>>num; Hash[num]=num; } int fn; while(m--) { cin>>fn; if(Hash[fn]!=-1) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }