美文网首页
1078 Hashing(hash散列)

1078 Hashing(hash散列)

作者: virgilshi | 来源:发表于2018-10-02 21:45 被阅读0次

1078 Hashing (25 分)

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 (≤10​4​​ ) 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 -

分析

本题考查hash散列,采取平方探测再散列解决冲突。

#include <iostream>
#include <vector>
using namespace std;
int main(){
    int msize,n;
    cin>>msize>>n;
    while(1){
        bool flag=false;
        for(int i=2;i*i<=msize;i++){
            if(msize%i==0){
                flag=true;
                break;
            }
        }
        if(flag==true) msize++;
        else break;
    }
    if(msize==1) msize=2;
    // cout<<"msize="<<msize<<endl;
    vector<bool> v(msize,false);

    int isfirst=0;
    for(int i=0;i<n;i++){
        int val;
        cin>>val;
        int j=0;
        while(j<msize&&v[(val+j*j)%msize]!=false) j++;
        if(j==msize){
            isfirst++==0?printf("-"):printf(" -");
        }else{
            isfirst++==0?printf("%d",(val+j*j)%msize):printf(" %d",(val+j*j)%msize);
            v[(val+j*j)%msize]=true;
        }
    }
    return 0;
}

相关文章

  • 1078 Hashing(hash散列)

    1078 Hashing (25 分) The task of this problem is simple: i...

  • Hashing(散列)

  • IOS 逆向开发(二)密码学 HASH

    1. HASH算法简介 1.1 HASH是什么? Hash算法(也叫散列算法) Hash,一般翻译做“散列”,也有...

  • 散列 & 线性散列

    Hashing 散列 原理: use key value to compute page address of t...

  • 散列表

    概念 散列表的实现常常叫做散列(hashing)。散列是一种用于以常数平均时间执行插入、删除和查找的技术。散列函数...

  • PAT 甲级 刷题日记|A 1078 Hashing (25

    1078 Hashing (25 分) 单词积累 Quadratic probing (with positiv...

  • 密码安全

    本文介绍密码安全相关的加密与散列算法。 目录 散列算法 加密算法对称加密非对称加密 散列算法 Hashing 是使...

  • 散列 Hash

    一、什么是散列? 散列 Hash是和顺序、链接和索引一样,是存储集合或者线性表的一种方法。 散列的基本思想是:以集...

  • 加密函数,加密手段。

    密码散列函数: 密码散列函数(英语:Cryptographic hash function),又译为加密散列函数、...

  • App安全与加密下

    Hash (散列函数) 1.Hash定义 Hash:把任意长度的输入,通过散列算法,变换成固定长度的输出,该输出就...

网友评论

      本文标题:1078 Hashing(hash散列)

      本文链接:https://www.haomeiwen.com/subject/fqiaoftx.html