1078 Hashing
来源:互联网 发布:窄斑凤尾蛱蝶 标本淘宝 编辑:程序博客网 时间:2024/05/16 12:29
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 -
解题思路:坑爹的二次探测,一开始以为是一道很简单的题,结果还是被坑了,完全没有注意到冲撞处理策略,所以最后一个点一直过不了,后来看了别人的博客说要经过二次探测检测之后还是不能插入才输出“-”,不是一开始不能插入就直接输出“-”。too young
二次冲撞检测就是,在i位置无法插入的情况下,进行i+1^2,i+2^2,i+3^2….依次递增的位置检测,如果检测到可以插入那就插入,一直找不到才输出“-”,网上别人定的范围是到30000^2,自己发现到10000^2也可以过,关于这个范围的指定不是很清楚为什么。
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;bool isPrime(int n){ if (n < 2){ return false; } for (int i = 2; i*i <= n+1; i++){ if (n%i == 0){ return false; } } return true;}int main(){ //10007 int primes[10050]; int per_prime = 0; for (int i = 10049; i >= 0; i--){ if (isPrime(i)){ per_prime = i; primes[i] = i; } else{ primes[i] = per_prime; } } for (int m, n; scanf("%d%d", &m, &n) != EOF;){ int size = primes[m]; int *flag = new int[size]; memset(flag, 0, sizeof(int)*size); int test = 0; while (n > 1 ){ int temp; test = 0; scanf("%d", &temp); for (int i = 0; i <= 10000; i++){ if (!flag[(temp + i*i) % size]){ flag[(temp + i*i) % size] = 1; test = 1; printf("%d ", (temp + i*i) % size); break; } } if (!test){ printf("- "); } n--; } if (n){ int temp; test = 0; scanf("%d", &temp); for (int i = 0; i <= 10000; i++){ if (!flag[(temp + i*i) % size]){ flag[(temp + i*i) % size] = 1; test = 1; printf("%d\n", (temp + i*i) % size); break; } } if (!test){ printf("-\n"); } } } return 0;}
- 1078 Hashing
- PAT 1078 Hashing (25)
- pat 1078 Hashing
- PAT 1078 Hashing
- pat 1078 Hashing
- pat甲 1078 Hashing
- PAT 甲级 1078 Hashing
- PAT 1078-Hashing (25)
- Hashing
- Hashing
- Hashing
- Hashing
- Hashing
- Hashing
- Pat(Advanced Level)Practice--1078(Hashing)
- PAT (Advanced Level) Practise 1078 Hashing (25)
- PAT (Advanced Level) Practise 1078 Hashing (25)(平方探测)
- PAT-1078 Hashing (散列表 二次探测法)
- 浙大PAT 1065 A+B and C (64bit) (20)
- UVA 10881(p9)----Piotr's Ants
- 2015年终总结
- hdoj Clarke and MST 5627(求位运算and后得到的最大生成树)(并查集&位运算)好题
- Qt创建新文件
- 1078 Hashing
- 极客学院-通过"Android学习之Launcher源码学习"看UML序列图
- DatabaseError:database disk image is malformed的解决方法
- iOS Apps 开发(Swift)(9)——Implement Navigation
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xb7 in position 7: ordinal not in range(128)
- Loadrunner重要概念——事务
- 在spark环境中运行demo的时候报错
- MSSQL数据库连接池
- [MEMO] Linux Kernel Debugging Training 琐碎(补充ing)