pat 1078. Hashing (25)

1078. Hashing (25)

时间限制
100 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 -

//题意,给定一个table大小,和要进行hash的数字,根据函数h(key) = key%table_size来进行hash。给你的table_size
//的大小有可能是质数有可能不是。如果不是质数,那么就找到一个大于table_size的最小质数作为table_size。哈希函数一
//般都会有冲突,如果发生冲突用二次探测正的增量法来解决冲突。这里涉及到一个知识,就是二次探测法是啥,对于前面的函数
// h(key) = key%table_size,如果h(key)和之前的冲突,那么就h2(key) = (key +di)%table_size,di = 1^2,-1^2
//,2^2,-2^2...有时候使用解决的方法都不管用。然而什么时候判断不管用呢?我是加到30000^2后就跳出,发现这样能过,其实
//和是否使用long long 没关系,下面我把所有整型都改为int,也过了

#include <stdio.h>
#define MAX 30000
int isPrime[MAX];
int myHash[MAX];
void initial()
{
	
	for(int i=0;i<MAX;i++)
	{
		isPrime[i] = 1; // is all prime
		myHash[i] = -1;
	}
	isPrime[1] = 0;
	for(int i=2;i<MAX/2+1;i++)
	{
		for(int j=i;i*j<MAX;j++)
		{
			isPrime[i*j] = 0;
		}
	}
	return ;
}
int main()
{
	initial();
	int n=0,i=0,m=0,j=0;
	int num,mSize;
	scanf("%d%d",&mSize,&n);
	if(isPrime[mSize]==0)
	{
		for(i=mSize;isPrime[i]==0;i++);
		mSize = i;
	}
	for(i=0;i<n;i++)
	{
		scanf("%d",&num);
		int solved = 0;
			for(j=0;j<=MAX;j++) //二次探测
			{
				m = (num + j*j)%mSize;
				if(myHash[m]==-1)
				{
					myHash[m] = num;
					printf("%d",m);
					solved = 1;
					break;
				}
			}
			if(solved==0)
			{
				printf("-");
			}
		if(i!=n-1)
		{
			printf(" ");
		}else
		{
			printf("\n");
		}
	}
	return 0;
}



//最后一个case段错误,不明白什么原因,改了两次都是断错误
//把m改成long long后得到答案错误,应该是数组越界了
//把所有的整形都改成long long仍然没有得到正确答案
//改了探测的写法,仍然没有过。把探测数值改到30000就过了。线性探测是每次加 n^2,n=1,2,3,4...


时间 结果 得分 题目 语言 用时(ms) 内存(kB) 用户
7月25日 14:57 答案正确 25 1078 C++ (g++) 7 632 jack

测试点

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 2 632 12/12
1 答案正确 2 632 3/3
2 答案正确 2 512 5/5
3 答案正确 7 512 5/5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值