今天做了一道平方探测法处理散列函数冲突的题:
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 (≤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 -
题意很简单,但是自己的做法竟然过不了最后一组的样例
原因在于,自己理解错了平方探测法的含义和它解决冲突时的做法
本来自己的理解是:假设第一次hash的位置为pos,若pos有元素,就依次尝试pos+/-1,pos+/-4,pos+/-9.....直到超出了存储长度,这样就算宣告插入失败
现在看来,自己的理解错误在了一个地方,当pos有元素时,应继续尝试(pos+/-1)%maxsize,(pos+/-4)%maxsize,(pos+/-9)%maxsize......,当探测了maxsize-1次之后仍没能找到插入位置的,视为插入失败
int pos=num%m;
for(int i=0;i<m-1;i++){
if(!vis[(i*i+pos)%m]){
vis[(i*i+pos)%m]=1;
printf("%d",(i*i+pos)%m);
flag=true;
break;
}
}
if(!flag) printf("插入失败!\n");
赶紧温习一下另一道平方探测法:
1145 Hashing - Average Search Time (25分)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find whether or not the key is in the table). 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 3 positive numbers: MSize, N, and M, which are the user-defined table size, the number of input numbers, and the number of keys to be found, respectively. All the three numbers are no more than 104. Then N distinct positive integers are given in the next line, followed by M positive integer keys in the next line. All the numbers in a line are separated by a space and are no more than 105.
Output Specification:
For each test case, in case it is impossible to insert some number, print in a line X cannot be inserted.
where X
is the input number. Finally print in a line the average search time for all the M keys, accurate up to 1 decimal place.
Sample Input:
4 5 4
10 6 4 15 11
11 4 15 2
Sample Output:
15 cannot be inserted.
2.8
这道题也是用平方探测法,但要求输出的时平均比较时间
如果比到一个位置发现没有值后,应该立即退出,因为这说明要找的元素不存在!还有就是,比较maxsize次后,还应该多比较一次(超出maxsize的一次),这也就是为什么样例中 找15用了6(maxsize+1=5+1)次,而不是5(maxsize=5)次
#include<bits/stdc++.h>
using namespace std;
int vis[100005];
bool judge(int num){
if(num<=1) return false;
for(int i=2;i<=int(sqrt(1.0*num));i++){
if(num%i==0) return false;
}
return true;
}
int n,m,k;
int main(){
vector<int>ANS;
int num;
cin>>m>>n>>k;
while(!judge(m)) m++;
for(int i=0;i<n;i++){
cin>>num;
bool flag=false;
for(int i=0;i<m;i++){
if(!vis[(i*i+num)%m]){
vis[(i*i+num)%m]=num;
flag=true;
break;
}
}
if(!flag) ANS.push_back(num);
}
double cnt=0;
for(int i=0;i<k;i++){
cin>>num;
bool flag=false;
int pos=num%m;
for(int i=0;i<=m;i++){
cnt++;
if(vis[(pos+i*i)%m]==num||!vis[(pos+i*i)%m]){
flag=true;
break;
}
}
}
for(int i=0;i<ANS.size();i++){
printf("%d cannot be inserted.\n",ANS[i]);
}
printf("%.1lf\n",cnt/k);
return 0;
}