Codevs 1230 STL万岁。。 。
给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出现过。
第一行两个整数 n 和m。
第二行n个正整数(1<=n<= 100000)
第三行m个整数(1<=m<=100000)
一共m行,若出现则输出YES,否则输出NO
4 2
2 1 3 4
1 9
YES
NO
所有数据都不超过10^8
分类标签 Tags 点此展开
哈希60分RE代码 head数组开小了
#include <iostream>
#include <cstring>
#include <cstdio>
#define mo1 12421
#define mo2 34343
using namespace std; struct node
{
int next;
int to;
}e[];
int n,m,i,j,tot,head[];
void add(int u,int v)
{
tot++;
e[tot].next=head[u];
e[tot].to=v;
head[u]=tot;
}
int get_hash(int k)
{
int h=;
while(k)
{
h=h*+k%;
k/=;
}
return h%mo2;
}
bool query(int u,int v)
{
for(int i=head[u];i;i=e[i].next)
{
if(e[i].to==v)
return true;
}
return false;
}
int main()
{
int a;
cin>>n>>m;
while(n--)
{
cin>>a;
int y=get_hash(a);
add(a,y);
}
while(m--)
{
cin>>a;
int y=get_hash(a);
if(query(a,y))
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
哈希满分做法
#include <iostream>
#include <cstring>
#include <cstdio>
#define mo1 12421
#define mo2 34343
using namespace std; struct node
{
int next;
int to;
}e[];
int n,m,i,j,tot,head[];
void add(int u,int v)
{
tot++;
e[tot].next=head[u];
e[tot].to=v;
head[u]=tot;
}
int get_hash(int k)
{
int h=;
while(k)
{
h=h*+k%;
k/=;
}
return h%mo2;
}
bool query(int u,int v)
{
for(int i=head[u];i;i=e[i].next)
{
if(e[i].to==v)
return true;
}
return false;
}
int main()
{
int a;
cin>>n>>m;
while(n--)
{
cin>>a;
int y=get_hash(a);
add(a,y);
}
while(m--)
{
cin>>a;
int y=get_hash(a);
if(query(a,y))
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
STL满分做法
#include<map>
#include<iostream>
using namespace std;
int s[];
map<int,bool>g;
int main()
{
int n,m,ss;
cin>>n>>m;
for(int i=;i<=n;++i)
{
cin>>s[i];
g[s[i]]=;
}
for(int i=;i<=m;++i)
{
cin>>ss;
if(g[ss]==)
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return ;
}
Codevs 1230 STL万岁。。 。的更多相关文章
- Codevs 1860 最大数 string大法好,STL万岁。。
题目描述 Description 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 输入描述 Input Description 第一行一个正整数n. 第二行n个正整数,空格隔开 ...
- codevs 1230 元素查找
题目链接:http://codevs.cn/problem/1230/ 题解: 会有很多方法写这道题,写个裸的哈希练练手 #include<cstdio> ,MOD=; int n,m,h ...
- codevs 1230【pb_ds】
题目链接[http://codevs.cn/problem/1230/] 题意:给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出现过. 题解:很简单的一道题,可以选择用 ...
- [Codevs 1230]元素查找(手写哈希表)
题目连接:http://codevs.cn/problem/1230/ 说白了就是要我们自己手写一个哈希表的数据结构来实现加入和查找功能.map也能直接过(我第一次写就是用map骗AC的) 提一下个人 ...
- AC日记——元素查找 codevs 1230
1230 元素查找 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 给出n个正整数,然后有 ...
- 元素查找(codevs 1230)
1230 元素查找 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 给出n个正整数,然后有m个询问,每 ...
- codevs——1230 元素查找
时间限制: 1 s 空间限制: 128000 Ks 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 给出n个正整数,然后有m个询问,每个询问一个整数 ...
- STL || HDU 1894 String Compare
如果一个词包含再另一个词的前面(前缀),是一对前缀,求一共有多少对 *解法:STL万岁 #include<string>:https://www.cnblogs.com/SZxiaochu ...
- 基础算法学习以及$STL$的使用
1.优先队列 (1)大根堆(小顶堆) priority_queue<int,vector<int>,greater<int> >q; (2)小根堆(大顶堆) pri ...
随机推荐
- Hierarchyid 常用操作
---------内置函数------------ select hierarchyid::GetRoot()--0x select hierarchyid::Parse('/1/1/') - ...
- Java Se :Map 系列
之前对Java Se中的线性表作了简单的说明.这一篇就来看看Map. Map系列的类,并不是说所有的类都继承了Map接口,而是说他们的元素都是以<Key, Value>形式设计的. Dic ...
- linux性能监控工具
1.uptime 该命令直观的显示了服务器在过去15分钟,5分钟,1分钟内的平均负载 2.vmstat 每隔2秒输出vmstat的信息,共输出10次. 类别 procs swap io sys ...
- PHP debug 环境配置
在建立PHP开发调试环境时,经常会遇到xdebug无法成功安装的问题,其实主要原因有两点: 1. xdebug版本和php版本不匹配 2.xdebug和 zend不能同时运行,需要在php.ini中禁 ...
- Activity中使用Intent实现页面跳转与参数的传递(转)
新建一个FirstAvtivity.java package com.zhuguangwei; import android.app.Activity; import android.content. ...
- hw 要的是螺丝钉
日前突然接到华为HR的电话,叫我去面试。本来我的工作和工资收入等各方面在本地也还算可以,没有想要跳槽。但是本着去看看有没有更好机会的想法就去了。 9:30到了现场后,在那里等了很久,一个考官上来问了 ...
- linux 下 mysql 安装(不用编译的方式)
环境是centos6.x.相信其他的也大同小异.相对来说这种方式我用的比较多. 一些环境依赖表库: yum install perl yum install libaio 1)下载:在mysql的网站 ...
- 树莓派搭建安装mysql
最近刚入手了一枚树莓派,突发奇想打算做一个小型的家用服务器,在家7*24小时一直挂着. 真的是非常小,只有巴掌大,给树莓派买了一些配件,外壳.小风扇.2片散热片.32G SD卡.HDMI线,组装之后的 ...
- 一:Shell基础
1.shell概述 shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用shell来启动,挂起,停止甚至是编写一些程序: shell还是 ...
- js判断手机访问PC端跳转到手机站
<script type="text/javascript">(function() { //得到域名后缀 var path = location.pathname.s ...