1078. Hashing (25)
来源:互联网 发布:大数据相关技术 编辑:程序博客网 时间:2024/06/01 10:13
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 410 6 4 15Sample Output:
0 1 4 -
#include <stdio.h>#include <stdlib.h>#include <math.h>#include <memory.h>int isPrime(int x){int i;if (x == 1){return 0;}for (i = 2; i <= sqrt(x); i++){if (x%i == 0){return 0;}}return 1;}int getSize(int M){int i;i = M;while (!isPrime(i)){i++;}return i;}int *a;int M, N;int table[15000];int insert(int x){int index;int i;i = 0;index = x%M;memset(table, 0, sizeof(table));while (a[index] != 0){if (table[index]++){return -1;}index = index + 2 * (++i) - 1;while(index >= M){index -= M;}}a[index] = x;return index;}int main(){int i, temp;//freopen("d:\\input.txt", "r", stdin);scanf("%d%d", &M, &N);M = getSize(M);a = (int*)calloc(M,sizeof(int));for (i = 0; i < N; i++){scanf("%d", &temp);temp = insert(temp);if (temp < 0){printf("-");if (i != N - 1){printf(" ");}}else{printf("%d", temp);if (i != N - 1){printf(" ");}}}return 0;}
- 1078. Hashing (25)
- PAT 1078. Hashing (25)
- pat 1078. Hashing (25)
- PAT 1078. Hashing (25)
- PAT 1078. Hashing (25)
- 1078. Hashing (25)
- 1078. Hashing (25)
- 1078. Hashing (25)
- 1078. Hashing (25)
- 1078. Hashing (25)
- PAT 1078. Hashing (25)
- PAT 1078. Hashing (25)
- 1078. Hashing (25)
- PAT 1078. Hashing (25)
- 1078. Hashing (25)
- 【PAT】1078. Hashing (25)
- 1078. Hashing (25)
- 1078. Hashing (25)
- 笔记3
- 机器学习笔记之牛顿方法
- Sqoop教程(一) Sqoop数据迁移工具
- python 通过post方式上传文件到php服务器
- 数据挖掘中的时序数据分析方法(According to Eamonn Keogh’s Time Series Tutorial)
- 1078. Hashing (25)
- sevureCRT乱码
- 24. Swap Nodes in Pairs
- JavaScript判断对象数组是否包含对象
- Fragment的用法
- GIT远程仓库基本操作(1)
- 命令模式c++实现
- 深入浅出Mybatis-改造Cache
- 异常Throwable(Exception/Error)