1230 元素查找

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 钻石 Diamond
 
 
题目描述 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

两种哈希基本写法:

//哈希表

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm> #define MAXN 1000008
#define MOD 1000007 using namespace std; int hashTable[MAXN]; void update(int x) //将数字x加入哈希表
{
int num=x;
x%=MOD;
while()
{
if(!hashTable[x])
{
hashTable[x]=num;
return;
}
if(hashTable[x]!=num)
{
x++;
if(x==MAXN) x=;
}
else return;
}
} bool query(int x) //查找哈希表中是否有数字x
{
bool found=false;
int num=x;
x%=MOD;
while()
{
if(!hashTable[x]) return false;
if(hashTable[x]!=num)
{
x++;
if(x==MAXN) x=;
}
else return true;
}
} int main()
{
int n,m,x;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&x);
update(x+);
}
for(int i=;i<=m;i++)
{
scanf("%d",&x);
if(query(x+)) printf("YES\n");
else printf("NO\n");
}
return ;
}

心若向阳,无言悲伤

//单模建边(这个题数据水)

#include<iostream>
using namespace std;
const int maxn=;
int n,m,tot,tmp,head[maxn];
struct node
{
int to;
int next;
}e[maxn];
int get_hash(int x)
{
return x%;
}
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 ;
return ;
}
int main()
{
int x,y;
cin>>n>>m;
for(int i=;i<=n;i++)
{
cin>>tmp;
x=get_hash(tmp);
add_edge(x,tmp);
}
for(int i=;i<=m;i++)
{
cin>>tmp;
x=get_hash(tmp);
if(find(x,tmp))
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return ;
}

心若向阳,无言悲伤

map水过...

#include<cstdio>
#include<iostream>
#include<map>
using namespace std;
map<int,int>ma;
int n,m,a[],x;
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
ma[a[i]]=;
}
for(int i=;i<=m;i++)
{
cin>>x;
if(ma[x]) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return ;
}

心若向阳,无言悲伤

codevs1230元素查找(hash)的更多相关文章

  1. 【哈希表】CodeVs1230元素查找

    一.写在前面 哈希表(Hash Table),又称散列表,是一种可以快速处理插入和查询操作的数据结构.哈希表体现着函数映射的思想,它将数据与其存储位置通过某种函数联系起来,其在查询时的高效性也体现在这 ...

  2. codevs1230 元素查找

    1230 元素查找  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 给出n个正整数,然后有m个询问,每 ...

  3. 【哈希表】CODEVS1230 元素查找

    #include<cstdio> #include<vector> using namespace std; typedef vector<int>::iterat ...

  4. Codevs_1230_元素查找_(set/Hash)

    描述 http://codevs.cn/problem/1230/ ... 1230 元素查找 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond       题目 ...

  5. jQuery 的选择器常用的元素查找方法

    jQuery 的选择器常用的元素查找方法 基本选择器: $("#myELement")    选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myE ...

  6. jQuery元素查找方式

    jQuery常用的元素查找方法总结 $("#myELement")    选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到 ...

  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. 浅析jQuery中常用的元素查找方法总结

    本篇文章是对jQuery中常用的元素查找方法进行了详细的总结和介绍,需要的朋友参考下   $("#myELement") 选择id值等于myElement的元素,id值不能重复在文 ...

随机推荐

  1. printf函数压栈(i++/i--,++i/--i) 终极解密

    #include <stdio.h> void main() { ; printf("%d %d %d %d\n", i, --i, i, i--); } 输出是“3 ...

  2. <MyBatis>入门四 传入的参数处理

    1.单个参数 传入单个参数时,mapper文件中 #{}里可以写任意值 /** * 传入单个参数 */ Employee getEmpById(Integer id); <!--单个参数 #{} ...

  3. 常用的HTTP测试工具谷歌浏览器插件汇总

    网页的开发和测试时最常见的测试就是HTTP测试,作为曾经的测试人员在这方面还是略知一二的.其实做网页测试工作是非常繁琐的时期,有时候甚至是无聊重复的,如果没有网页测试工具的帮助的话,测试人员会越做越怀 ...

  4. TensorFlow — 相关 API

    TensorFlow — 相关 API TensorFlow 相关函数理解 任务时间:时间未知 tf.truncated_normal truncated_normal( shape, mean=0. ...

  5. visioStudio常见问题

    问题一: 在做项目时候,使用VisioStudio 2008,一不小心将设置恢复到了原始,一直找不到需要的东西. 比如生成方式“debug”和“Release”选择框没有.一些图标也没有. 经过不断的 ...

  6. noip模拟赛 柜(暴力)

    分析:暴力的方法是非常显然的,从起点走一次,从终点走一次,路径相交的点即为所求,但是这样存图都很难存下,而且如果数据极端可能要求R*C次,时间空间都受不了.如果不需要记录整张图,并且一次能移动很多步就 ...

  7. hdu 2602 简单0-1背包模板题

    #include<stdio.h> #include<string.h> #define N 1100 int dp[N]; int main() { int n,t,m,a[ ...

  8. cpus Vs cpu period and cpu quota

    1.  https://docs.oracle.com/cd/E37670_01/E75728/html/section-zb1_wqw_gt.html To control a container' ...

  9. nyoj_278_排队_201403282135

    排队 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 周末了,软件ACM的队员准备玩玩游戏,娱乐一下,CY想了一个好主意,所有队员站成一个圈,从1开始报数,凡是报出指 ...

  10. NOIP2014 提高组合集

    NOIP 2014 提高组 合集 D1 T1 生活大爆炸版石头剪刀布 首先,先将两个人的猜拳序列都变得不小于n.然后逐个模拟.胜败什么的看表就行了. #include <iostream> ...