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. swift 集成使用最新版百度地图_v2.10.2(一)

    目前在开发中使用百度地图的APP越来越多了,我在网上找的集成百度地图的例子不是很多,于是我就将我集成百度地图的过程记录了下来: 一.前提:安装CocoaPods sudo gem install co ...

  2. 洛谷——P3946 ことりのおやつ(小鸟的点心)

    P3946 ことりのおやつ(小鸟的点心) 题目太长,请去链接里看吧 注意细节:特判终点(即使困住又能怎样,到达就好了),特判高度 #include<bits/stdc++.h> #defi ...

  3. [HNOI]2003 消防局的建立

    消防局的建立 本题地址:http://www.luogu.org/problem/show?pid=2279 题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料 ...

  4. noip模拟赛 戏

    [问题背景]zhx 和他的妹子(们) 做游戏.[问题描述]考虑 N 个人玩一个游戏,任意两个人之间进行一场游戏(共 N*(N-1)/2 场),且每场一定能分出胜负.现在, 你需要在其中找到三个人构成“ ...

  5. hdu 4670 树的分治-求点对的个数

    /* 树的分治 因为树的点权值可达到10^15,注意手动扩栈,还有int64 题意:给你一棵树,给你一些素数,给你每个点一个权值且每个权值均可由这些素数组成.现在定义任意任意两点的价值为他们路径上的权 ...

  6. 生成随机数验证码的工具类(from韩顺平)

    生成随机数验证码的工具类 package com.cx; //生成随机数的图片 import java.awt.Color; import java.awt.Font; import java.awt ...

  7. git巧妙命令行

    git cherry-pick c7081607cfd1bfa99b6e6c70c208e71fbd8767ae

  8. C. Hexadecimal's Numbers

    C. Hexadecimal's Numbers time limit per test 1 second memory limit per test 64 megabytes input stand ...

  9. 中文命名之Hibernate 5演示 - 使用注解(annotation)而非xml定义映射

    前文中文编程:中文命名之Hibernate 4+MySQL演示最后留下了个Hibernate 5之后出现的问题, 于是在Hibernate社区提交了报告: Seemingly regression s ...

  10. spring mvc参数校验

    一.在SringMVC中使用 使用注解 1.准备校验时使用的JAR validation-api-1.0.0.GA.jar:JDK的接口: hibernate-validator-4.2.0.Fina ...