首页 > 其他分享 >Hashing (25)

Hashing (25)

时间:2022-09-26 00:55:07浏览次数:50  
标签:prime 25 hash int msize table size 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 (<=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.


输出描述:

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.题目要求只取正向增量,若要考虑负向增量还需自己编写一个取模函数

2.平方探测法插入失败的条件是p>msize/2

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int arr[10005];
 4 int hash_table[100005]; //由于可能需要msize增加到质数,所以稍微开大一点
 5 int msize,n;
 6 /* 题目只需正增量,直接取余即可
 7 int mod(int a,int b){ //取模
 8     if (a>=0)
 9         return a%b;
10     return (abs(a/b)+1)*b+a;
11 }*/
12 bool prime(int x){
13     if (x==1)
14         return false;
15     if (x==2)
16         return true;
17     for (int i=2;i<=x/2;++i){
18         if (x%i==0){
19             return false;
20         }
21     }
22     return true;
23 }
24 
25 int insert(int x){
26     int key=x%msize;
27     int p=0;
28     while(p<=msize/2){
29         int index=(key+p*p)%msize;
30         if (hash_table[index]==-1){ //该空间未被占用
31             hash_table[index]=x;
32             return index;
33         }
34         ++p;
35     }
36     return -1;
37 }
38 int main(){
39     cin>>msize>>n;
40     memset(hash_table,-1,sizeof(hash_table));
41     for (int i=0;i<n;++i){
42         cin>>arr[i];
43     }
44     while(!prime(msize)){
45         msize++;
46     }
47     for (int i=0;i<n;++i){
48         int position=insert(arr[i]);
49         if (position==-1)
50             cout<<"-";
51         else
52             cout<<position;
53         if (i!=n-1)
54             cout<<" ";
55     }
56 }

 

标签:prime,25,hash,int,msize,table,size,Hashing
From: https://www.cnblogs.com/coderhrz/p/16729533.html

相关文章

  • 2022年9月25日
       如果事与愿违,上天必另有安排!   失之东隅,收之桑榆。  上帝为你关了一扇门,也会为你打一扇窗!   一切都是命运最好的安排!  希望9月底,10月份可以......
  • 9.25 小记
    CF804D期望+树的直径+根号分治调完了一道题(手残\(sz->siz\)找半天)题解差分约束都在这里了废话早上五点多就醒了,然后在肯德基等早餐等了好久感觉周日很不安的样子......
  • 正经人谁记日记 2022-09-25 周日 21:51:39
    做时间的主人书是人类进步的阶梯......
  • 9.25模拟赛
    T1数数贪个心,比较显然T2数树考虑求出不合法的部分,然后做容斥,但是容斥系数我不会配T3鼠树大nb题单点权实际上就是它的归属点的权,动态维护每个点的归属点是比较好做......
  • 9月25学习总结
    第一周总结一、typora软件与markdown语法1.typora软件typora是一个文本编辑器,文本阅读器,是现今最火爆的文本编辑器。Typora中格式,字体,主题样式都是可以根据自己的喜好......
  • 闲话?2020.9.25
    //闲话的真正形态是什么?我至今为止的可以称之为闲话的博都有在“雕琢”(用输入法打you第一个字是幼,不愧是我。),但这是否违背了闲话的初衷?一篇闲话会用VScode写上一两......
  • 2022-09-25-近60道MySQL经典面试题
    近60道MySQL经典面试题mysql面试常见问题学习整理2.3.17.18.19.20.44未看。1.B树和B+树之间的区别是?为什么mysql使用B+树?一个节点有多个元素;B+树也是排序了的;B+树非叶......
  • 【闲话】2022.09.25
    考试++虽然我个人习惯是++i。hereRSY大佬大家快去关注\(\texttt{_RSY_}\)大佬!顺便记得不要像我一样写题解的时候老爱加空格。失踪人口回归L回来了(说起来L......
  • [Oracle] LeetCode 253 Meeting Rooms II 优先队列
    Givenanarrayofmeetingtimeintervalsintervalswhereintervals[i]=[starti,endi],returntheminimumnumberofconferenceroomsrequired.Solution我们用......
  • 9.25
    本周学习总结1.计算机基础2.typora软件和markdown语法3.python解释器和环境4.pycharm5.PEP8规范计算机基础1.计算机的本质​ 计算机也称之为"电脑">>>:通电的大脑......