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 ...
随机推荐
- jQuery删除节点和追加节点
for (var i in checkedBoxIds) { var $td = $("#" + checkedBoxIds[i]).parent().parent().detac ...
- Getting Started with ASP.NET Web API 2 (C#)
By Mike Wasson|last updated May 28, 2015 7556 of 8454 people found this helpful Print Download Com ...
- WPF学习之路(三) 属性与依赖
类型是DependencyProperty的属性是依赖属性 依赖属性不同于普通的.Net属性,类似于一个计算过程,根据依赖的值得到最终值. 为什么引入依赖属性: MSDN原文 One of the p ...
- EF框架学习手记
转载: [ASP.NET MVC]: - EF框架学习手记 1.EF(Entity Framework)实体框架EF是ADO.NET中的一组支持开发面向数据的软件应用程序的技术,是微软的一个ORM框架 ...
- 持续集成(CI)初探
前不久接触了持续集成(Continuous Integration,CI). 一.持续集成是什么 首先说说“集成”的概念.在实际的软件开发中,常常会发生两种情境: 1.几个项目组对同一个系统的不同功能 ...
- VS 2013中的新特性browser link
Browser Link是连接VS和浏览器之间的通道.有了这个特性,web程序就能够和VS交互传递数据.这个特性在VS2013中是默认开启的.当开启了Browser Link, web程序运行的时候, ...
- .net开发windows服务小结
今天学习了在.net下创建一个windows服务,总结一下学习心得. 开发环境:visual studio 2012 一.编写程序 (1)创建一个空解决方法 (2)添加一个控制台应 ...
- Sql Server之旅——第十站 看看DML操作对索引的影响
我们都知道建索引是需要谨慎的,当只有利大于弊的时候才适合建,我们也知道建索引是需要维护成本的,这个维护也就在于DML操作了, 下面我们具体看看到底DML对索引都有哪些内幕.... 一:delete操作 ...
- zookeeper barrier和queue应用实例
package org.windwant.zookeeper; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper. ...
- 万能面试问题大全,教你怎么回答,怎么拿下offer
一.你对薪资的要求? 回答提示: 说实话,大家找工作,都希望找个高薪的,那我们如何和公司去谈薪酬呢?如果你对薪酬的要求太低,那显然贬低自己的能力:如果你对薪酬的要求太高,那又会显得你分量过重,公司受用 ...