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 ...
随机推荐
- MetaWeblog API调用
http://rpc.cnblogs.com/metaweblog/webenh 在网上闲逛,突然对博客的接口感兴趣,经考察,多数博客都对metaWeblog Api 提供了支持,虽然windows ...
- yii2 输出xml格式数据
作者:白狼 出处:http://www.manks.top/yii2_xml_response.html.html本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文 ...
- Linux 虚拟机网络适配器从E1000改为VMXNET3
我们知道VMware的网络适配器类型有多种,例如E1000.VMXNET.VMXNET 2 (Enhanced).VMXNET3等,就性能而言,一般VMXNET3要优于E1000,下面介绍如果将Lin ...
- Mysql查询阻塞初探
第一次值班,报警打电话给我说,数据库复制延时一个多小时,那个时候是半夜啊,但我还是很清醒的起来,开机.vpn.登录.show processlist,结果发现情况是这样的: 红线框表示的是当前每个线程 ...
- FATAL ERROR: Could not find ./bin/my_print_defaults的解决办法
对mysql数据库初始化过程中,会有如下报错信息: 1 2 3 4 5 6 7 [root@localhost scripts]# ./mysql_install_db --user=mysql FA ...
- SQL Server开发接口生成方法
为提高开发效率,生成固定格式的接口是必须的,以下以提供新增/修改/删除/读取接口为例: 以常见的表结构为例,特殊表结构可自己尝试去调整方法 主要通过系视图 sys.columns生成方法:为包含列的对 ...
- Fatal error: Call-time pass-by-reference has been removed
下面的代码报错:Fatal error: Call-time pass-by-reference has been removed function myFunc($arg) { do somethi ...
- Codevs 1025 选菜
题目描述 Description 在小松宿舍楼下的不远处,有PK大学最不错的一个食堂--The Farmer's Canteen(NM食堂).由于该食堂的菜都很不错,价格也公道,所以很多人都喜欢来这边 ...
- 关于Leetcode上二叉树的算法总结
二叉树,结构很简单,只是比单链表复杂了那么一丢丢而已.我们先来看看它们结点上的差异: /* 单链表的结构 */ struct SingleList{ int element; struct Singl ...
- 301重定向.htaccess规则(含二级目录跳转二级域名)
301重定向是一种非常重要的"自动转向"技术.网址重定向最为可行的一种办法.当用户或搜索引擎向网站服务器发出浏览请求时,服务器返回的HTTP数据流中头信息(header)中的状态码 ...