1145. Hashing - Average Search Time (25)
时间限制 200 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 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 two 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. 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
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <cmath>
const int Maxn = 100010;
int N, M, Size;
bool hashTable[Maxn] = { false }; //标记是否已经拆入了数据
int StepNum[Maxn] = { 0 }; //记录插入或者查找需要的步数,如果值为0,表示下标值没有插入到hash表
bool isPrime(int data) {
if (data <= 1) return false; //如果小于等于1肯定不是素数
if (data == 2) return true;
int Sqr = sqrt((double)data);
for (int i = 3; i <= Sqr; ++i) {
if (!(data % i)) return false;
}
return true;
}
int getTableSize(int size) {
while (!isPrime(size)) ++size;
return size;
}
bool InsertElem(int data) {
int step = 1;
while (step <= Size) {
int n = step - 1;
int key = (data + n * n) % Size;
if (hashTable[key] == false) {
hashTable[key] = true;
break;
}
++step;
}
StepNum[data] = step;//记录插入或者查找data需要的步数
return (step > Size) ? false : true; //如果插入的步数大于表长Size,那么肯定插入失败,否则成功
}
int getStepNum(int data) {
if (StepNum[data]) return StepNum[data];
else { //一定是没有插入的数据,那么找到一个没有插入的位置或者查找次数大于表长就查找结束
int step = 1;
while (step <= Size) {
int n = step - 1;
int key = (data + n * n) % Size;
if (!hashTable[key]) return step;
++step;
}
return step;
}
}
int main() {
#ifdef _DEBUG
freopen("data.txt", "r+", stdin);
#endif // _DEBUG
int size;
scanf("%d %d %d", &size, &N, &M);
Size = getTableSize(size); //获取表长
for (int i = 0; i < N; ++i) {
int data; scanf("%d", &data);
if (!InsertElem(data)) printf("%d cannot be inserted.\n", data);
}
double stepSum = 0;
for (int i = 0; i < M; ++i) {
int data; scanf("%d", &data);
stepSum += getStepNum(data);
}
printf("%.1f", stepSum / M);
return 0;
}