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. <MyBatis>入门五 查询的返回值处理

    select : 返回对象:  <select  id = " "  resultType= "对象的全类名"  /> List: <sele ...

  2. python3返回值中的none

    浏览器返回null,python3返回none,懵了. google了很多资料,不明就里,这就是没基础的后果啊呀呀呀. 上阮一峰的截图,就这么理解下凑合吧:

  3. HTML-js 压缩上传的图片方法(默认上传的是file文件)

    //压缩图片方法 function compressImg(file,callback){ var src; var fileSize = parseFloat(parseInt(file['size ...

  4. pandas处理各类表格数据

    经常遇到Python读取excel和csv还有其他各种文件的内容.json还有web端的读取还是比较简单,但是excel和csv的读写是很麻烦.这里记录了pandas库提供的方法来实现文本内容和Dat ...

  5. 离职 mark

    昨天(2019 年 5 月 17 日),从 离职. 从 2018 年 7 月 14 日早 10 点余分到 2019 年 5 月 17 日早 10 点余分,一共 308 天整.这就是我出学校的第一份工作 ...

  6. 【04】JSONP 教程

    JSONP 教程 Jsonp(JSON with Padding) 是 json 的一种"使用模式",可以让网页从别的域名(网站)那获取资料,即跨域读取数据. 为什么我们从不同的域 ...

  7. 九度oj 题目1046:求最大值

    题目1046:求最大值 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:11782 解决:4789 题目描述: 输入10个数,要求输出其中的最大值. 输入: 测试数据有多组,每组10个数. ...

  8. RabbitMQ-基本概念(一)

    整体架构模型 Producer 消息生产者,生产者创建消息然后发布到RabbitM中,消息一般包含2个部分 消息体(payload)和标签 消息体就是带有业务逻辑结构的数据,消息标签用来表述这条消息, ...

  9. fetch api & response header

    how to get fetch response header in js https://stackoverflow.com/questions/43344819/reading-response ...

  10. noip模拟赛 读

    分析:感觉很像是贪心,但是直接贪找不到方法.一个暴力的想法是枚举最小步数,然后看每个指针能够覆盖到的位置,看看能不能覆盖到所有点.这个求最大覆盖就有点贪心的思想,因为给的ai,bi都是递增顺序的,考虑 ...