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≤10?4??) 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[0] = prime[1] = 1;
    for(i = 2; i < MAX; i++){
        if(0 == prime[i]){
            for (j = i * i; j < MAX; j += i) {
                prime[j] = 1;
            }
        }
    }
}

void Hashing(){
    int M, N, i, j, val;

    memset(hash, -1, MAX * sizeof(int));
    scanf("%d%d", &M, &N);
    while (0 != prime[M]) {
        M++;
    }
    for (i = 0; i < N; i++) {
        scanf("%d", &val);
        for (j = 0; j < M; j++) {
            if(-1 == hash[(val % M + j * j) % M]){
                hash[(val % M + j * j) % M] = val;
                if(0 == i){
                    printf("%d", (val % M + j * j) % M);
                }
                else{
                    printf(" %d", (val % M + j * j) % M);
                }
                break;
            }
        }
        if(j == M){
            if(0 == i){
                printf("-");
            }
            else{
                printf(" -");
            }
        }
    }
}

int main() {
    Make_Prime();
    Hashing();
}
时间: 2024-10-03 23:53:53

PTA Hashing的相关文章

ERROR&lt;53761&gt; - Plugins - conn=-1 op=-1 msgId=-1 - Connection Bind through PTA failed (91). Retrying...

LDAP6.3在DSCC控制台启动实例完成,但是操作状态显示“意外错误”,查看日志如下: 04/May/2016:21:10:39 +0800] - Sun-Java(tm)-System-Directory/6.3 B2008.0311.0224 (32-bit) starting up[04/May/2016:21:10:39 +0800] - Listening on all interfaces port 11111 for LDAP requests[04/May/2016:21:10

一致性哈希算法(consistent hashing)(转)

原文链接:每天进步一点点——五分钟理解一致性哈希算法(consistent hashing) 一致性哈希算法在1997年由麻省理工学院提出的一种分布式哈希(DHT)实现算法,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正了CARP使用的简 单哈希算法带来的问题,使得分布式哈希(DHT)可以在P2P环境中真正得到应用. 一致性hash算法提出了在动态变化的Cache环境中,判定哈希算法好坏的四个定义: 1.平衡性(Balance):平衡性是指哈希的

Hashing图像检索源码及数据库总结

下面的这份哈希算法小结来源于本周的周报,原本并没有打算要贴出来的,不过,考虑到这些资源属于关注利用哈希算法进行大规模图像搜索的各位看官应该很有用,所以好东西本小子就不私藏了.本资源汇总最主要的收录原则是原作者主页上是否提供了源代码,为了每种方法的资料尽可能完整,本小子会尽可能的除提供源码下载地址外,还会给出PDF文章的链接.项目主页,slide等. 对哈希方法重新进行调研,右图是找到的提供有部分源码的哈希方法,这其中包含了比较经典的哈希方法,比如e2lsh.mih,同时也包含有最近几年一直到13

PTA 5-8(English) File Transfer (25) - 并查集 - 数组实现

题目:http://pta.patest.cn/pta/test/16/exam/4/question/670 PTA - Data Structures and Algorithms (English) - 5-8 We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one computer t

Go语言实现一致性哈希(Consistent Hashing)算法

一致性哈希可用于解决服务器均衡问题. 用Golang简单实现了下,并加入了权重.可采用合适的权重配合算法使用. package main //一致性哈希(Consistent Hashing) //author: Xiong Chuan Liang //date: 2015-2-20 import ( "fmt" "hash/crc32" "sort" "strconv" "sync" ) const DE

[Webpack 2] Hashing with Webpack for long term caching

Leveraging the browser cache is an important part of page load performance. A great way to utilize this cache is by versioning your resources. In this lesson, learn how to use Webpack’s hashing feature so you can take advantage of long term caching o

PTA Huffman Codes

题目重现 In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redundancy Codes", and hence printed his name in the history of computer science. As a professor who gives the final exam problem on Huffman codes, I am

1078. Hashing (25)【Hash + 探测】——PAT (Advanced Level) Practise

题目信息 1078. Hashing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B 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(

一致性 hash 算法( consistent hashing )a

一致性 hash 算法( consistent hashing ) 张亮 consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在cache 系统中应用越来越广泛: 1 基本场景 比如你有 N 个 cache 服务器(后面简称 cache ),那么如何将一个对象 object 映射到 N 个 cache 上呢,你很可能会采用类似下面的通用方法计算 object 的 hash 值,然后均匀的映射到