PTA 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 \% TSizeH(key)=key%TSize where TSizeTSize 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: MSizeMSize (\le 10^4≤104) and NN (\le MSize≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then NN 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 -
解答
这题主要是生成素数的算法,通过Make_Prime函数实现,还有就是平方探测开放寻址时,当用于探测的量j(每次拿j * j探测)等于Hash表的Size的时候,待插入的数还没有插入的话,就可以判断出待插入的数将永远无法插入。
//
// main.c
// Hashing
//
// Created by 余南龙 on 2016/12/8.
// Copyright © 2016年 余南龙. All rights reserved.
//
#include <stdio.h>
#include <string.h>
#define MAX 10008
int prime[MAX], hash[MAX];
void Make_Prime(){
int i, j;
prime[] = prime[] = ;
; i < MAX; i++){
== prime[i]){
for (j = i * i; j < MAX; j += i) {
prime[j] = ;
}
}
}
}
void Hashing(){
int M, N, i, j, val;
memset(hash, -, MAX * sizeof(int));
scanf("%d%d", &M, &N);
!= prime[M]) {
M++;
}
; i < N; i++) {
scanf("%d", &val);
; j < M; j++) {
== hash[(val % M + j * j) % M]){
hash[(val % M + j * j) % M] = val;
== i){
printf("%d", (val % M + j * j) % M);
}
else{
printf(" %d", (val % M + j * j) % M);
}
break;
}
}
if(j == M){
== i){
printf("-");
}
else{
printf(" -");
}
}
}
}
int main() {
Make_Prime();
Hashing();
}
PTA Hashing的更多相关文章
- PTA 11-散列2 Hashing (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/679 5-17 Hashing (25分) The task of this pro ...
- PTA 11-散列4 Hard Version (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/680 5-18 Hashing - Hard Version (30分) Given ...
- [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 ...
- 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 ...
随机推荐
- Hibernate使用
实现类: public class InfoDAOImpl extends BaseDao<Info> 1.List<Object[]> midlist=super.creat ...
- C++设计模式-State状态模式
State状态模式作用:当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. UML图如下: State类,抽象状态类,定义一个接口以封装与Context的一个特定状态相关的行为. ...
- 2、SIP
1.初学者笔记:http://www.cnblogs.com/gnuhpc/archive/2012/01/16/2323637.html 2.SIP头字段解释:http://www.cnblogs. ...
- urlparse
urlparse模块 urlparse主要是URL的分解和拼接,分析出URL中的各项参数,可以被其他的URL使用,而且只在python2.7中存在,python3中是在urllib包下的urllib. ...
- Android自动化学习笔记:获取APK包名的几种方法
---------------------------------------------------------------------------------------------------- ...
- 500lines项目简介
"500行或更少" "What I cannot create, I do not understand." -- Richard Feynman <50 ...
- Excel 取得一定范围内最大的有值的行号
dim iRow iRow = Range("A1000").End(xlUp).Row
- crontab 提示 command not found 解决方案
今天遇见一个问题,crontab的定时任务会报错:java command not found,但是手动执行脚本一直能成功. 猜想是环境变量的问题. 在crontab里添加个打印环境变量的任务: * ...
- arguments转换为数组格式
var agArr = []; for(var i = 0; i < arguments.length; i++) { agArr.push(arguments[i]) }
- 通过SQL Server 2008数据库复制实现数据库同步备份
SQL Server 2008数据库复制是通过发布/订阅的机制进行多台服务器之间的数据同步,我们把它用于数据库的同步备份.这里的同步备份指的是备份服务器与主服务器进行 实时数据同步,正常情况下只使用主 ...