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 ...
随机推荐
- 如何退出调起多个Activity的Application?
1.记录打开的Activity 每打开一个activity,即记录下来,需要关闭时,关闭每一个activity即可. 2.发送特定的广播 在需要结束应用时,发送一个特定广播,每个activity收到此 ...
- 谈谈vertical-align的text-bottom和text-top - 韦奕
学习资料 : http://www.tuicool.com/articles/uuYvMv 友情链接 行高 替换元素,非替换元素,内联元素,块元素
- mysql源码解读之事务提交过程(二)
上一篇文章我介绍了在关闭binlog的情况下,事务提交的大概流程.之所以关闭binlog,是因为开启binlog后事务提交流程会变成两阶段提交,这里的两阶段提交并不涉及分布式事务,当然mysql把它称 ...
- 问题解决——VC 断点 无效 一个可能情况?
=================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载 请通过右 ...
- 五、Android学习第四天补充——Android的常用控件(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 五.Android学习第四天补充——Android的常用控件 熟悉常用的A ...
- 完整卸载 kubuntu-desktop from Ubuntu 14.04 LTS
系统 ubuntu 14.04 LTS 64Bit 目的:卸载kubuntu-desktop 方法一: sudo apt-get remove libkde3support4 k3b-data ntr ...
- 嵌入式Linux开发板
嵌入式Linux开发板开发介绍: iTOP-4412嵌入式Linux开发板搭载三星Exynos四核处理器,配备1GB内存,4GB固态硬盘EMMC存储,独家配备三星S5M8767电源管理,配备Andro ...
- jquery back to top 页面底部的返回顶部按钮
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [转][前端优化]使用Combres合并对js、css文件的请求
本文转自:http://www.cnblogs.com/parry/archive/2011/01/28/Reduce_Http_Request_Using_Combres_For_Js_Css.ht ...
- 荒芜的周六-PHP之面向对象(三)
hi 又是开森的周六了.积攒的两周的衣服,终于是差不多洗完了.大下午的才来学点东西~~ 1.PHP面向对象(三) 四.OOP的高级实践 4.3 Static-静态成员 <?phpdate_def ...