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. C++11 Thread多线程的学习心得与问题

    C++11 ,封装了thread的多线程的类,这样对多线程的使用更加方便. 多线程的原理我不加赘述,可以参看操作系统等参考书. 多线程代码可以最大化利用计算机性能资源,提高代码的运行效率,是常用优化方 ...

  2. [luogu4056 JSOI2009] 火星藏宝图 (贪心 dp)

    传送门 Solution 一个显然的贪心:选的点数越多越好.这个随便推推就知道了. 那么我们就贪心的从一列上挑最靠下的转移 直接转移不斜率优化复杂度\(O(nm)\),吸一口O2过了... Code ...

  3. foreach获取访问元素的下标

    今天在用foreach循环的时候,要同时根据访问下标获取另一个list对象数据,之前想的方法是加一个变量i,然后每次i++,但是感觉这样不是很好!,后来发现这样也可以!举个简单的例子! foreach ...

  4. Java基础学习总结(77)——Java枚举再总结

    在Java SE5之前,我们要使用枚举类型时,通常会使用static final 定义一组int常量来标识,代码如下 public static final int MAN = 0; public s ...

  5. nyoj 63 小猴子下落

    小猴子下落 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 有一颗二叉树,最大深度为D,且所有叶子的深度都相同.所有结点从左到右从上到下的编号为1,2,3,····· ...

  6. Codeforces Round #235 (Div. 2)

    A. Vanya and Cards time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. hdu 1853 KM算法

    #include<stdio.h> #include<math.h> #include<string.h> #define N 200 #define inf 99 ...

  8. Spring 进行junit单元测试时,出现method ‘initializationError’ 错误

    首先检查一下所有的方法是否为public 然后看是否有commons-logging这个日志包

  9. jd-eclipse插件的安装

    一,资源 jd-eclipse-site-1.0.0-RC2.zip    百度网盘链接:https://pan.baidu.com/s/1GTFFY_1jg4k9vjZNE4JliQ       提 ...

  10. Linux的基本配置

    安装: vmvare .centos .SecureCRTPortable(免装版) 软件地址:http://pan.baidu.com/s/1eSBqegq 密码:p4ck 3.开始配置 1打开vm ...