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的更多相关文章

  1. [Algorithm] 局部敏感哈希算法(Locality Sensitive Hashing)

    局部敏感哈希(Locality Sensitive Hashing,LSH)算法是我在前一段时间找工作时接触到的一种衡量文本相似度的算法.局部敏感哈希是近似最近邻搜索算法中最流行的一种,它有坚实的理论 ...

  2. Consistent hashing —— 一致性哈希

    原文地址:http://www.codeproject.com/Articles/56138/Consistent-hashing 基于BSD License What is libconhash l ...

  3. 一致性 hash 算法( consistent hashing )a

    一致性 hash 算法( consistent hashing ) 张亮 consistent hashing 算法早在 1997 年就在论文 Consistent hashing and rando ...

  4. PTA Hashing

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  5. PAT1078 Hashing

    11-散列2 Hashing   (25分) The task of this problem is simple: insert a sequence of distinct positive in ...

  6. Feature hashing相关 - 1

    考虑典型的文本分类,一个经典的方法就是     分词,扫描所有特征,建立特征词典 重新扫描所有特征,利用特征词典将特征映射到特征空间编号 得到特征向量 学习参数 w 存储学习参数 w , 存储特征映射 ...

  7. 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 ...

  8. Indexing and Hashing

    DATABASE SYSTEM CONCEPTS, SIXTH EDITION11.1 Basic ConceptsAn index for a file in a database system wo ...

  9. 用单分子测序(single-molecule sequencing)和局部敏感哈希(locality-sensitive hashing)来组装大型基因组

    Assembling large genomes with single-molecule sequencing and locality-sensitive hashing 好好读读,算法系列的好文 ...

  10. guava学习--hashing

    128位的MurmurHash(烽火使用过): 看一下Java标准库中的非加密哈希算法你会发现少了MurmurHash,这是一个简单高效且还是分布式的算法,在许多语言中都有着很好的支持.我们并不是说要 ...

随机推荐

  1. Pytest框架中,conftest.py文件的作用?

    conftest.py文件,它主要是实现fixture共享的. 第一,conftest.py文件当中,它储存的都是fixture,就是给用例提供做前置准备工作和后置清理工作的一个东西: 第二,conf ...

  2. NB-IoT成为3GPP后会有哪些优势

    NB-IoT无线接入的设计使用了很多LTE设计大的原则,并且得到了传统蜂窝网络和芯片组供应商的支持,使MBB取得了成功.NB-IoT采用与LTE(E-UTRA)相同的设计原则,尽管它使用单独的新载波, ...

  3. RabbitMQ相关概念的理解

    1.什么是消息? 消息就是程序(服务)之间传递的数据(图/文/声/像). 2.MQ是什么? MQ(MessageQueue)是指消息队列亦或消息总线.是消息的容器,这个容器的策略是FIFO(先进先出) ...

  4. Docker(7)- docker images 命令详解

    如果你还想从头学起 Docker,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1870863.html 作用 列出所有的本地镜像 语法格 ...

  5. 使用IDEA完成一个SpringBoot的demo

    打算开始做毕业设计了,写一些博客记录一下做毕业设计的过程. 前两天从老师那里拿了学长学姐做的非常简陋的代码,配置环境跑了一下,老师找我的时候说还剩下50%的工作,但感觉至少还有70%. 废话不多说,今 ...

  6. 基于synchronized锁的深度解析

    1. 问题引入 小伙伴们都接触过线程,也都会使用线程,今天我们要讲的是线程安全相关的内容,在这之前我们先来看一个简单的代码案例. 代码案例: /** * @url: i-code.online * @ ...

  7. 百度地图省市php获取

    $api = 'http://api.map.baidu.com/shangquan/forward/?qt=sub_area_list&ext=1&level=3&areac ...

  8. leetcode68-search-in-rotated-sorted-array-ii

    题目描述 继续思考题目 "Search in Rotated Sorted Array": 如果数组种允许有重复元素怎么办? 会影响时间复杂度吗?是怎样影响时间复杂度的,为什么? ...

  9. STM32最小系统板OLED贪吃蛇

    上次用STM32F103最小系统板做了一个简单的OLED贪吃蛇小游戏,以下为游戏效果动图: 主要实现内容包括:贪吃蛇移动.方向控制.食物生成.分数处理.死亡判定. 这次想把自己的制作思路分享给大家,不 ...

  10. map,filter

    def is_odd(n): return n % 2 == 1 newlist = filter(is_odd, [i for i in range(0,5)]) print(list(newlis ...