题目如下
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 -
题目分析
题目是典型的二次探测解决哈希冲突的问题。并且题目减少了难度,主要是正向探测,没有反向探测!!
题目处理流程
1.如果不冲突。记下位置。
2.如果冲突。二次探测,如果二次探测过程中不冲突,记下位置,否则转向3
3.找不到位置,位置=“-”
4.把记录的位置输出。
题目难点
1.注意是,正向二次探测,不是全部探测
2.不能用字符串记录位置。否则一直格式错误。也不知道哪里错了,如果有知道请留言回复。(代码中注释的部分)。用数组记录就不冲突,奇怪不!
代码如下
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <sstream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
bool isPrime(int x){
bool flag=true;
if(x==1){
flag=false;
}
if(x>2){
for(int i=2;i<x;i++){
if(x%i==0){
flag = false;
break;
}
}
}
return flag;
}
int transfer(int x){
for(int i = x+1;i<10010;i++){
if(isPrime(i)){
return i;
}
}
}
string toString(int x){
string st;
stringstream ss;
ss << x;
ss >> st;
return st;
}
int main(int argc, char *argv[]) {
int Msize,N;
scanf("%d%d",&Msize,&N);
if(!isPrime(Msize)){
Msize = transfer(Msize);
}
//cout<<Msize<<endl;
int num[N];
int position[Msize];
int result[Msize];
int count=0;
for(int i=0;i<Msize;i++){
position[i] = -1;
result[i]= -1;
}
for(int i=0;i<N;i++){
scanf("%d",&num[i]);
}
string s;
//cout<<s<<endl;
for(int i=0;i<N;i++){
int t = num[i] % Msize;
bool flag = false;
if(position[t]==-1){
position[t] = num[i];
flag = true;
//s = s+toString(t);
result[count++]=t;
//cout<<s<<endl;
}
else{
//cout<<t<<endl;
int index = 1;
while(index<=Msize/2){
int temp = (t+index*index)%Msize;
//cout<<temp<<endl;
if(position[temp]==-1){
position[temp]=num[i];
s = s+toString(temp);
result[count++]=temp;
flag = true;
break;
}
else{
index++;
}
}
}
if(!flag){
s = s+"-";
result[count++]=-2;
}
}
if(result[0]==-2)
cout<<"-";
else
cout<<result[0];
for(int i=1;i<count;i++){
if(result[i]==-2){
cout<<" "<<"-";
}
else{
cout<<" "<<result[i];
}
}
//用字符串输出,就是错误
//if(s.length()==1){
// //string s1 = ;
// cout<<s.substr(0,1);
// }
// else{
// // printf("%c",s.at(0));
// cout<<s.substr(0,1);
// for(int i=1;i<s.length();i++){
// //printf(" %c",s.at(i));
// cout<<" "<<s.substr(i,1);
// }
// //printf("\n");
// }
return 0;
}