[PAT A1078]Hashing

[PAT A1078]Hashing

题目描述

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.

输入格式

Each input file contains one test case. For each case, the first line contains two positive numbers: MSize (≤10​4​​) 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.

输出格式

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.

输入样例

4 4
10 6 4 15

输出样例

0 1 4 -

解析

  1. 这道题目对于知识面不够深的同学们可能就很难受了,因为要知道Hash以及Hash解决冲突的办法,再就是英文单词得认识(这里我我前面意思看得明白,到后面就看不明白了,结果是看了中文理解了题意才一遍完成的,还是得提高自己的英语水平)
  2. 首先呢,输入两个数,分别是Msize和N,分别代表原始的Hash表的大小和要存入Hash表的数的个数。但是这个题目不满意,Msize必须为比它大的最小的素数,例如这道题目,Msize是4,所以数组Hash表空间得开到5,要不然就死在第一道坎上了。
  3. 其次,这里有一个关键词叫做quadratic probing(二次探测法),这就需要一点关于hash的知识了,例如hash开到5,首先输入10,10%5=0,那么我们就把10存到第0个位置。然后又输入一个数,15,15%5=0,但是0位置我们已经存放了10了,那么我们就要使用(15+12),(15-12),(15+22),(15-22)依次类推(注意这里题目要求了with positive increments only 意思就是只要大于15的,小于15的不做考虑),直到有一个位置能放下15为止,例如这里15+12=16,,16%5=1,那么我们就把15放到1这个位置
  4. 然后最主要一点就是,什么时候终止循环,因为如果一直循环下去都找不到安放这个数的位置的话,这就是一个死循环,这里可以证明,当a+t2中的t达到Msize-1时还找不到可以放它的位置的话,以后的t同样也找不到放它的位置,具体证明就不叙述了。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool isprime(int n) //判断素数
{
 if (n <= 1) return false;
 for (int i = 2; i*i <= n; i++) {
  if (n%i == 0) return false;
 }
 return true;
}
int main()
{
 int Msize, N, temp;
 vector<int> primes; //素数表
 scanf("%d%d", &Msize, &N);
 for (int i = 2; i <= 10100; i++) {  //由于Msize<10000,这里我们以防万一,从0计算到10100的素数
  if (isprime(i)) primes.push_back(i);
 }
 Msize = primes[upper_bound(primes.begin(), primes.end(), Msize)-primes.begin()];
 vector<int> hash(Msize, -1);
 for (int i = 0; i < N; i++) {
  scanf("%d", &temp);
  int t = 0, pos;
  while (t < Msize) {
      pos = (temp + t*t) % Msize;
   if (hash[pos] == -1)break;
   t++;
  }
  if (hash[pos] == -1) {
   hash[pos] = temp;
   printf("%d", pos);
  }
  else printf("-");
  if (i != N - 1) printf(" ");
 }
 return 0;
}
水平有限,如果代码有任何问题或者有不明白的地方,欢迎在留言区评论;也欢迎各位提出宝贵的意见!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值