pat 甲级 1078. Hashing (25)

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 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 (<=104) 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 -

思路:主要是平方探测的定义。若H(key)%size的位置已经被占用,接下来要查询的那些位置是(H(key)+k*k)%mod 其中k>=0&&k<size。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 11000+5
#define MSIZE 11000+5
int is_prime[MSIZE];
int prime[N_MAX];
int sz, m;
int a[N_MAX];
int pos[N_MAX];
int vis[N_MAX];
int seive(int n) {
    int p = 0;
    fill(is_prime, is_prime + n, 1);
    is_prime[0] = is_prime[1] = 0;
    for (int i = 2; i <= n; i++) {
        if (is_prime[i]) {
            prime[p++] = i;
            for (int j = i*i; j <= n; j += i) {
                is_prime[j] = 0;
            }
        }
    }
    return p;
}

int main() {
    int num = seive(MSIZE);
    while (cin >> sz >> m) {
        for (int i = 0; i < m; i++) scanf("%d", &a[i]);
        if (!is_prime[sz])
            for (int i = 0; i < num; i++) {
                if (sz < prime[i]) {
                    sz = prime[i];
                    break;
                }
            }

        for (int i = 0; i < m; i++) {
            int Pos = a[i],tmp=Pos;
                bool flag = 0;
                for (int k = 0; k < sz;k++) {
                    Pos =(tmp+ k*k)%sz;
                    if (vis[Pos] == 0) {
                        vis[Pos] = 1;
                        pos[i] = Pos;
                        flag = true;
                        break;
                    }
                }
                if (!flag)pos[i] = -1;
        }
        for (int i = 0; i < m; i++) {
            if (pos[i] != -1)cout << pos[i];
            else cout << "-";
            printf("%c", i + 1 == m ? '\n' : ' ');
        }
    }
    return 0;
}

 

posted on 2018-03-09 20:29  ZefengYao  阅读(117)  评论(0编辑  收藏  举报

导航