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 ...
随机推荐
- sqlserver2000 在查询时产生序号列的办法
用的是数据库sqlserver2000,唉,有点老了,好处是到处都有,安装方便. select ( select count(*) from temp_gzsphzb as t1 where spid ...
- notepad++ 编辑xml的插件和使用方法
notepad++ 编辑xml的插件和使用方法.mark http://blog.csdn.net/wangnan537/article/details/48712233
- Nopcommerce 二次开发0
Nopcommerce 是国外的一个高质量的开源b2c网站系统,基于EntityFramework6.0和MVC5.0,使用Razor模板引擎,有很强的插件机制,包括支付配送功能都是通过插件来实现的 ...
- VBA中如何动态定义数组
利用 dim Arr()as string这样声明,一旦赋值后,数组大小也就固定了.如果要改变数组大小,要用redim 命令redim arr(10) 加preserve 可以不清空数组,保持原有数据 ...
- 15,SFDC 管理员篇 - 变更和部署
Setup | Deploy | Sandbox 通过在Production上建立Dev .Test 两套环境来 Sandbox 有四种类型 Developer Sandbox & Dev ...
- ReactNative win10初体验
根据RN中文网上的文档搭建环境,有的地方说的不是很清楚,所以小白的我搞了很久,终于搭建成功,这里分享一下 1. java: jdk和jre,我下载的是1.8(因为官方文档上说AndroidStudio ...
- C#有关数组内存的释放及动态数组问题
一.数组内存释放问题 数组内存的释放可以按照如下语句实现: string [] aa=new string[2]; aa[0]="A"; aa[1]="B"; ...
- bash 相关的一些小代码片断
批量修改文件名: for i in *.html; do mv $i ${i/oldstring/newstring}; done; 批量替换文件内容: sed -i "s/oldstrin ...
- Spring使用非applicationContext.xm 默认名的配置文件的配置
Spring默认的配置文件是applicationContext.xml,但是有些时候,希望拆分Spring的配置文件,让其单一化,每一个都只进行自己的配置,如图所示 那么就需要在web.xml中配置 ...
- Cacti:添加监控磁盘IO
来自:http://blog.sina.com.cn/s/blog_61c07ac50101gkzp.html 1.检查net-snmp是否支持IO监控 snmpwalk -v 1 -c public ...