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

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

  2. PTA 11-散列4 Hard Version (30分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/680 5-18 Hashing - Hard Version   (30分) Given ...

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

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

  4. Consistent hashing —— 一致性哈希

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

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

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

  6. PAT1078 Hashing

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

  7. Feature hashing相关 - 1

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

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

  9. Indexing and Hashing

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

随机推荐

  1. 如何使用PL/SQL进行远程数据库连接

    1.先找到Oracle数据库的安装地址然后找到network->ADMIN->tnsnames.ora文件 2.添加下面文件到tnsnames.ora文件中去,红色部分是需要修改的部分   ...

  2. android 程序代码执行adb

    Runtime.getRuntime().exec("adb pull /dev/graphics/fb0 C:/fb1"); Runtime. getRuntime().exec ...

  3. jquery ajax success 函数 异步调用方法中不能给全局变量赋值的原因及解决办法

    jquery ajax success 函数 异步调用方法中不能给全局变量赋值的原因及解决办法   在调用一个jquery的ajax方法时我们有时会需要该方法返回一个值或者给某个全局变量赋值,可是我们 ...

  4. mapreduce核心原理

    1:第一阶段: 第一阶段的task每一个nodemanager并发运行程序,完全并发,各不影响.这就是map阶段 运行的task时maptask 2:第二个阶段的task并发实例互不相干,但是他们的数 ...

  5. PSP个人项目耗时记录

    估计这个任务需要多少时间: 计划 估计用时 估计这个任务需要多少时间 500min 开发 450min 需求分析 120min 生成设计文档 20min 设计复审 30miin 代码规范 10min ...

  6. 数据库——DQL(语句查询)

    查询的基本结构: 概念: 查询(DQL)是数据库中最为常用和复杂的一种SQL结构,学会编写查询SQL是程序员必须具备的能力. 要从数据库中查询数据,我们要使用SQL的SELECT语句.标准SELECT ...

  7. 用adox 取 access 自增列

    百度很久 最后在 (.NET2.0下用ADOX动态创建ACCESS数据库(C#)) http://blog.csdn.net/black4371/article/details/4423739 找到了 ...

  8. JUC.Lock(锁机制)学习笔记[附详细源码解析]

    锁机制学习笔记 目录: CAS的意义 锁的一些基本原理 ReentrantLock的相关代码结构 两个重要的状态 I.AQS的state(int类型,32位) II.Node的waitStatus 获 ...

  9. ZYNQ 的PS GEM DMA存在缺陷

    使用iperf对zynq进行单socket tcp传输速率测试: 无网络损伤时,单向网络带宽约为600Mbps,双向网络带宽相加约400Mbps: 50ms延时,1ms抖动,无丢包时,单向网络带宽约为 ...

  10. 安装zabbix报错configure: error: libcurl library not found

    libcurl 和libcurl-devel都已经安装 且大于7.13.1 但还是报错,这时需要在configure 指定curl路径即可 ./configure --prefix=/usr/loca ...