题解
Quadratic probing 二次探测
处理冲突的方法:
开放定址法
Hi=(H(key)+di) MOD m i=1,2,...,k(k<=m-1)
其中m为表长,di为增量序列
如果di值可能为1,2,3,...m-1,称线性探测再散列。
如果di取值可能为1,-1,2,-2,4,-4,9,-9,16,-16,...k*k,-k*k(k<=m/2) 称二次探测再散列。
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int MAXN = 1e4 + 10;
bool isPrime(int n) {
if(n <= 1) return false;
for(int i = 2; i <= sqrt(n); ++i) if(n % i == 0) return false;
return true;
}
int Hash[MAXN];
int main() {
int tsize, n, x, step;
scanf("%d%d", &tsize, &n);
while(!isPrime(tsize)) ++tsize;
for(int i = 0; i < n; ++i) {
scanf("%d", &x);
int key = x % tsize;
if(!Hash[key]) {
Hash[key] = true;
printf("%d%c", key, i == n - 1 ? '\n' : ' ');
continue;
}
for(step = 1; step <= tsize / 2; ++step) {
key = (x + step * step) % tsize;
if(!Hash[key]) {
Hash[key] = true;
printf("%d%c", key, i == n - 1 ? '\n' : ' ');
break;
}
}
if(step == tsize / 2 + 1) printf("-%c", i == n - 1 ? '\n' : ' ');
}
return 0;
}