1078 Hashing

题意:给出表长和待插入的元素,求每个元素的插入位置,散列函数为H(key)=key%TSize,解决冲突利用平方探测法(只考虑正向偏移)。

思路:同1145 Hashing - Average Search Time

代码:

#include <cstdio>
#include <cmath>
#include <unordered_map>
using namespace std;

bool isPrime(int n)
{
    if(n<=1) return false;
    int sqr=(int)sqrt(n);
    for(int i=2;i<=sqr;i++)
        if(n%i==0) return false;
    return true;
}

int main()
{
    int MSize,n,key;
    scanf("%d%d",&MSize,&n);
    while(!isPrime(MSize))
        MSize++;
    bool exist[10005]={false};//exist[i]为true表示位置i已经被占据了
    int data[10005];
    unordered_map<int,int> pos;//pos[val]存放val的位置
    for(int i=0;i<n;i++){
        scanf("%d",&key);
        data[i]=key;
        int d=0;
        for(;d<MSize;d++){
            int p=(key+d*d)%MSize;
            if(exist[p]==false){
                exist[p]=true;
                pos[key]=p;
                break;
            }
        }
        if(d==MSize) pos[key]=-1;
    }
    for(int i=0;i<n;i++){
        if(pos[data[i]]==-1) printf("-");
        else printf("%d",pos[data[i]]);
        if(i<n-1) printf(" ");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/kkmjy/p/9550673.html

时间: 2024-10-12 05:08:17

1078 Hashing的相关文章

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(

1078. Hashing (25)-PAT甲级真题

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

PAT 1078. Hashing (25)

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

1078. Hashing (25)

时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

PAT 1078 Hashing

# include <stdio.h> # include <algorithm> # include <string.h> # include <iostream> # include <math.h> using namespace std; int prime[10100]; int vis[10110]; int cot; void init_prime() { memset(vis,0,sizeof(vis)); cot=0; for(

PAT (Advanced Level) 1078. Hashing (25)

二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using namespace std; const int

PAT Advanced 1078 Hashing (25) [Hash ?次?探查法]

题目 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

PAT1078. Hashing

1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 fun

pat解题报告【1078】

1078. Hashing (25) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 fun