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 (≤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. 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.
时间: 2023-11-22 17:52:31 浏览: 106
这道题的任务很简单:将一串不同的正整数插入哈希表中,并输出输入数字的位置。哈希函数的定义为 H(key)=key%TSize,其中 TSize 是哈希表的最大大小。使用二次探测(只增加正数)来解决碰撞。注意,表格大小最好为质数。如果用户给出的最大大小不是质数,则必须重新定义表格大小为大于用户给出大小的最小质数。
输入格式:每个输入文件包含一个测试用例。对于每种情况,第一行包含两个正数:MSize(≤10 4)和N(≤MSize),它们是用户定义的表大小和输入数字的数量。然后,在下一行中给出N个不同的正整数。一行中的所有数字由空格分隔。
输出格式:对于每个测试用例,在一行中打印输入数字的相应位置(索引从0开始)。一行中的所有数字由空格分隔,行末不能有多余的空格。如果无法插入数字,则打印“-”。
阅读全文