1078. Hashing (25)

来源:互联网 发布:东莞网络问政平台 编辑:程序博客网 时间:2024/06/15 13:41

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 15
Sample Output:
0 1 4 -
简单的hash表处理冲突,注意最多冲突次数的限制
#include <iostream>using namespace std;const int maxn = 20000;int table[maxn];int next_prime(int x){for(int i= (x < 2)?2:x;;i++){bool ok = true;for(int j=2;j * j <= i;j++){if(i % j == 0){ok = false;break;}}if(ok)return i;}}int main(){int p,n;cin>>p>>n;p = next_prime(p);int kase = 0;for(int i=0;i<n;i++){int t;cin>>t;bool ok = false;for(int j=0;j<=p/2;j++){int pos = (j*j + t%p)%p;if(table[pos] == 0){table[pos] = t;if(kase++)cout<<" ";cout<<pos;ok = true;break;}}if(ok == false){if(kase++)cout<<" ";cout<<"-";}}return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 触手tv直播黑屏怎么办 酷狗id密码忘记怎么办 打游戏网络不稳定怎么办 电脑打字法没了怎么办 家庭版密钥专业版系统怎么办 win7应用程序不能启动怎么办 win7用户密码忘记了怎么办 win7用户密码忘了怎么办 windows开不了机怎么办 网卡被卸载了怎么办 win7注销黑屏了怎么办 w7密码忘了怎么办 笔记本电脑键盘进水了怎么办 笔记本键盘进水了怎么办 笔记本进水键盘失灵怎么办 win7进不了系统怎么办 电脑显示屏两边黑屏怎么办 win8关机关不了怎么办 win10没激活黑屏怎么办 忘了产品密钥怎么办 小马易贷逾期怎么办 10系统未激活怎么办 win10账户被停用怎么办 win7激活码无效怎么办 windows显示不是正版怎么办 优酷上传错误怎么办 盗版系统会黑屏怎么办 安装了盗版系统怎么办 电脑鼠标不好用怎么办 自己组装电脑系统怎么办 ie双击没反应怎么办 电脑系统删了怎么办 产品密钥不知道怎么办 电脑安装ae崩溃怎么办 升级win10要密钥怎么办 系统里没有中文怎么办 小马激活后黑屏怎么办 电脑变成英文了怎么办 3dsmax英文版怎么办 手机设置越南文怎么办 电脑没有dvd驱动器怎么办