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,这是一个简单高效且还是分布式的算法,在许多语言中都有着很好的支持.我们并不是说要 ...
随机推荐
- AtCoder Grand Contest 013D: Piling Up 题解
题意简化: [luogu] Piling Up 一开始有n个颜色为黑白的球,但不知道黑白色分别有多少,m次操作,每次先拿出一个球,再放入黑白球各一个,再拿出一个球,最后拿出的球按顺序排列会形成一个颜色 ...
- python机器学习实现线性回归
线性回归 关注公众号"轻松学编程"了解更多. [关键词]最小二乘法,线性 一.普通线性回归 1.原理 分类的目标变量是标称型数据,而回归将会对连续型的数据做出预测. 应当怎样从一大 ...
- git版本管理系统使用
版本管理系统Git 关注公众号"轻松学编程"了解更多. git下载链接:https://pan.baidu.com/s/12vJn-K0lK9XlkVQbNe8S-A 密码:m4m ...
- Charles使用part1——基本功能介绍
一. 安装与破解: 官网地址:https://www.charlesproxy.com/download/ 破解自行解决. 二. 启动与配置: 启动 Charles 后,第一次 Charles 会请求 ...
- 配置交换机Trunk接口流量本地优先转发(集群/堆叠)
组网图形 Eth-Trunk接口流量本地优先转发简介 在设备集群/堆叠情况下,为了保证流量的可靠传输,流量的出接口设置为Eth-Trunk接口.那么Eth-Trunk接口中必定存在跨框成员口.当集群/ ...
- CI框架导入 excel
整合PHP Excel和PHPexcelReader到 librarys下面 两个excel整合成一个excel <?php class Excel extends Control ...
- 鸿蒙开发板外设控制 之 实现物理按键的“长按事件”(按键通用框架 V0.0.2)
我在之前的帖子<实现按键"按下事件"和"释放事件"的通用框架(V0.0.1)>中阐述了DTButton-V0.0.1的设计思路,并且也在帖子中开源了 ...
- C# Span 源码解读和应用实践
一:背景 1. 讲故事 这两天工作上太忙没有及时持续的文章产出,和大家说声抱歉,前几天群里一个朋友在问什么时候可以产出 Span 的下一篇,哈哈,这就来啦!读过上一篇的朋友应该都知道 Span 统一了 ...
- 1,github更新问题
赶去吃饭,不排版了. 一:上传 1,新建仓库 通过下面代码克隆仓库到本地: git clone https://github.com/ndnmonkey/zhihu.git 把文件放到克隆到本地的文件 ...
- notepad++覆盖了eclipse的快捷键
好长时间发现eclipse快捷键alt+/无法使用,今天决定解决一下 1.Windows Hotkey Explorer 用此工具找到是notepad++占用了快捷键 2.C:\Program Fil ...