7-1 Hashing
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be H(key)=key%TSize where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.
Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (<=104) and N (<=MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space. MSize (<=104) and N (<=MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.) and N (≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then N distinct positive integers are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the corresponding positions (index starts from 0) of the input numbers in one line. All the numbers in a line are separated by a space, and there must be no extra space at the end of the line. In case it is impossible to insert the number, print "-" instead.
Sample Input:
4 4
10 6 4 15
Sample Output:
0 1 4 -
题目大意:
给出大小为m的哈希表长,以及n个数。要求将元素按读入的顺序插入至哈希表中,并用二次探测法解决冲突问题,如果冲突无法解决则输出 -
解决思路:
先解决表长不为素数的问题,再解决哈希表的插入问题,二次探测法的公式为 (key + step * step) % size,该题最后会卡一下空格输出的格式
代码实现:
#include<iostream>
using namespace std;
int n,size;
int temp;
bool judge[10100]={false};
void cubeinsert(int key)
{
for(int i = 0; i < size; i ++)
{
int index = (key + i * i) % size;//二次探测法公式(key + step * step) % size
if(!judge[index])
{
cout<<index;
judge[index] = true;
return;
}
}
cout<<'-';
}
bool isprime(int n)//判断素数
{
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
int main()
{
cin>>size>>n;
while(!isprime(size))
{
size++;
} for(int i = 0; i < n; i ++)
{
cin>>temp;
if(i != 0) cout << ' ';//测试卡输出格式
cubeinsert(temp);
}
return 0;
}
7-1 Hashing的更多相关文章
- [Algorithm] 局部敏感哈希算法(Locality Sensitive Hashing)
局部敏感哈希(Locality Sensitive Hashing,LSH)算法是我在前一段时间找工作时接触到的一种衡量文本相似度的算法.局部敏感哈希是近似最近邻搜索算法中最流行的一种,它有坚实的理论 ...
- Consistent hashing —— 一致性哈希
原文地址:http://www.codeproject.com/Articles/56138/Consistent-hashing 基于BSD License What is libconhash l ...
- 一致性 hash 算法( consistent hashing )a
一致性 hash 算法( consistent hashing ) 张亮 consistent hashing 算法早在 1997 年就在论文 Consistent hashing and rando ...
- PTA Hashing
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- PAT1078 Hashing
11-散列2 Hashing (25分) The task of this problem is simple: insert a sequence of distinct positive in ...
- Feature hashing相关 - 1
考虑典型的文本分类,一个经典的方法就是 分词,扫描所有特征,建立特征词典 重新扫描所有特征,利用特征词典将特征映射到特征空间编号 得到特征向量 学习参数 w 存储学习参数 w , 存储特征映射 ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 H. Hashing
H. Hashing time limit per test 1 second memory limit per test 512 megabytes input standard input out ...
- Indexing and Hashing
DATABASE SYSTEM CONCEPTS, SIXTH EDITION11.1 Basic ConceptsAn index for a file in a database system wo ...
- 用单分子测序(single-molecule sequencing)和局部敏感哈希(locality-sensitive hashing)来组装大型基因组
Assembling large genomes with single-molecule sequencing and locality-sensitive hashing 好好读读,算法系列的好文 ...
- guava学习--hashing
128位的MurmurHash(烽火使用过): 看一下Java标准库中的非加密哈希算法你会发现少了MurmurHash,这是一个简单高效且还是分布式的算法,在许多语言中都有着很好的支持.我们并不是说要 ...
随机推荐
- day81:luffy:课程分类页面&课程信息页面&指定分类显示课程信息&分页显示课程信息
目录 1.构建课程前端初始页面 2.course后端的准备工作 3.后端实现课程分类列表接口 4.前端发送请求-获取课程分类信息 5.后端实现课程列表信息的接口 6.前端显示列表课程信息 7.按照指定 ...
- Typora + picgo + sm.ms 图床设置笔记
Typora + picgo + sm.ms 图床设置笔记 编辑于2020-03-26 本文部分内容在作者教程的基础上进行了二次编辑,如有重复,纯属必然 在此感谢大佬们的无私付出与分享 之前 用了 g ...
- Activit的心路历程:获取当前节点的上一节点【可能存在多个】的nodeId
在我的开发任务中,突然给我提出了一个待办任务需要获取当前任务节点上以任务节点的表单信息,刚开始搞得我有点措手不及,后来仔细是靠后,灵感一下,直接操作流程的bpmn信息就可以获取到节点信息嘛,顺着这个思 ...
- 第一行代码中RecyclerView添加依赖库问题
现在更新到 implementation 'com.android.support:recyclerview-v7:29.2.1' 记得点Sync Now来进行同步.
- python数据分析02语法基础
在我来看,没有必要为了数据分析而去精通Python.我鼓励你使用IPython shell和Jupyter试验示例代码,并学习不同类型.函数和方法的文档.虽然我已尽力让本书内容循序渐进,但读者偶尔仍会 ...
- 超值干货 | 建议收藏:精美详尽的 HTTPS 原理图注意查收!
作为一个有追求的程序员,了解行业发展趋势和扩充自己的计算机知识储备都是很有必要的,特别是一些计算机基础方面的内容,就比如本篇文章要讲的计算机网络方面的知识.本文将为大家详细梳理一下 HTTPS 的实现 ...
- 数论总结——更新ing
数论还是有很多没学完 只是小小的总结 一.同余定理 1.反身性:\(a\equiv a (mod m)\) 2.对称性:若\(a\equiv b(mod m)\),则\(b\equiv a (mod ...
- nginx优化【收藏篇】
1. 动态页返回码 2. 错误页面优化 3. 连接数优化 4.配置优化 5.日志优化 关于运维学习.分享.交流,笔者开通了微信公众号[大隆爱分享],感兴趣的朋友可以关注下,欢迎加入,建立属于我们自己的 ...
- 异常记录-Gradle依赖掉坑之旅
前言 最近在项目中遇到了一个问题,死活拉不下来依赖,耗费了一整天,感觉自己真是菜的抠脚. 没想到今天脑子一清醒,刷刷的问题逐个击破了. 问题描述: 项目成员添加了新的依赖,然后我这边项目拉下来,bui ...
- Android 架构组件-Lifecycle、LiveData、ViewModel
Lifecycle Lifecycle组件包括LifecycleOwner.LifecleObserver,能方便监听Activity或者Fragment的生命周期. 步骤: 1.实现Lifecycl ...