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 \% TSizeH(key)=key%TSize where TSizeTSize 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: MSizeMSize (\le 10^4≤10 4 ) and NN (\le MSize≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then NN 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 -
AC代码:
#include <iostream>
#include <cstring>
using namespace std;
class HashTable{
private:
int* num;
int len;
public:
HashTable(int len){
this->len = this->Get_MaxPrime(len);
num = new int[this->len];
//cout<<"长度为"<<this->len<<endl;
memset(num,0,sizeof(int)*this->len);
}
bool IsPrime(int n){
bool flag = true;
if ( n <= 1){
flag = false;
}else if (n == 2){
flag = true;
}else{
for ( int i = 2 ; i < n ; i++){
if ( n % i == 0){
flag = false;
break;
}
}
}
return flag;
}
int Get_MaxPrime(int n){
if ( n == 1 || n == 2){
return 2;
}else{
for ( int i = n ; ; i++){
if ( i % 2 == 0){
continue;
}else{
if (this->IsPrime(i)){
return i;
}
}
}
}
}
void Insert(int n){
int index = n % this->len;
if (!this->num[index]){
num[index] = n;
//cout<<n<<"所在的位置为:"<<index<<endl;
}else{
int next = -1;
for ( int i = 1 ; i <= this->len/2 ; i++){
next = (index + i * i) % this->len;
if (!this->num[next]){
this->num[next] = n;
break;
}
}
//cout<<n<<"所在的位置为:"<<next<<endl;
}
}
int Find(int n){
int result_index = -1;
for ( int i = 0 ; i < this->len ; i++){
if (this->num[i] == n){
result_index = i;
break;
}
}
return result_index;
}
};
int main()
{
int Max,N;
int* buf;
cin>>Max>>N;
buf = new int[N];
HashTable hashtable(Max);
for ( int i = 0 ; i < N ; i++){
cin>>buf[i];
hashtable.Insert(buf[i]);
}
for (int i = 0 ; i < N ; i++){
buf[i] = hashtable.Find(buf[i]);
}
if(buf[0] == -1){
cout<<"-";
}else{
cout<<buf[0];
}
for(int i = 1 ; i < N ; i++){
cout<<" ";
if(buf[i] == -1){
cout<<"-";
}else{
cout<<buf[i];
}
}
return 0;
}