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 ( 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 (≤) and N (≤) 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 -

 #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<math.h>
#define MAXTABLESIZE 20000 typedef int ElementType;
typedef enum { Legitimate, Empty, Deleted } EntryType;
typedef struct HashEntry Cell;
struct HashEntry
{
ElementType Data;
EntryType Info;
}; typedef struct HblNode* HashTable;
struct HblNode
{
int TableSize;
Cell* Cells;
}; int NextPrime(int N)
{
if (N == )
return ;
int p = (N % ) ? N + : N + ;
int i;
while (p<=MAXTABLESIZE)
{
for (i = (int)sqrt(p); i > ; i--)
if (p % i == )
break;
if (i == )break;
else
p += ;
}
return p;
}
int Hash(int Key, int TableSize)
{
return Key % TableSize;
}
HashTable CreateHashTable(int TableSize)
{
HashTable H;
H = (HashTable)malloc(sizeof(struct HblNode));
H->TableSize = NextPrime(TableSize);
H->Cells = (Cell*)malloc(H->TableSize * sizeof(Cell));
for (int i = ; i < H->TableSize; i++)
H->Cells[i].Info = Empty;
return H;
} int Find(HashTable H, ElementType Key)
{
int NewPos, CurPos;
int CNum = ;
NewPos = CurPos = Hash(Key, H->TableSize);
while (H->Cells[NewPos].Info!=Empty&&H->Cells[NewPos].Data!=Key)
{
++CNum;
int Flag = ;
NewPos = CurPos+CNum * CNum;
if (CNum>=H->TableSize)
return -;
while (NewPos >= H->TableSize)
NewPos -= H->TableSize;
}
return NewPos;
} int Insert(HashTable H, ElementType Key)
{
int Pos = Find(H, Key);
if (Pos ==-)
return -;
if (H->Cells[Pos].Info != Legitimate)
{
H->Cells[Pos].Data = Key;
H->Cells[Pos].Info = Legitimate;
}
return Pos;
} int main()
{
int M, N;
scanf("%d %d", &M, &N);
HashTable H = CreateHashTable(M);
int i;
for (i = ; i < N-; i++)
{
int num;
scanf("%d", &num);
int Pos = Insert(H,num);
if (Pos != -)
printf("%d ", Pos);
else
printf("- ");
}
int num;
scanf("%d", &num);
int Pos = Insert(H, num);
if (Pos != -)
printf("%d", Pos);
else
printf("-");
return ;
}

1078 Hashing (25分)的更多相关文章

  1. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  2. 【PAT甲级】1078 Hashing (25 分)(哈希表二次探测法)

    题意: 输入两个正整数M和N(M<=10000,N<=M)表示哈希表的最大长度和插入的元素个数.如果M不是一个素数,把它变成大于M的最小素数,接着输入N个元素,输出它们在哈希表中的位置(从 ...

  3. 1078. Hashing (25)【Hash + 探測】——PAT (Advanced Level) Practise

    题目信息 1078. Hashing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The task of this problem is simple: in ...

  4. pat 甲级 1078. Hashing (25)

    1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...

  5. PTA 11-散列2 Hashing (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/679 5-17 Hashing   (25分) The task of this pro ...

  6. PAT甲题题解-1078. Hashing (25)-hash散列

    二次方探测解决冲突一开始理解错了,难怪一直WA.先寻找key%TSize的index处,如果冲突,那么依此寻找(key+j*j)%TSize的位置,j=1~TSize-1如果都没有空位,则输出'-' ...

  7. 1078. Hashing (25)

    时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of this problem is simp ...

  8. 5-17 Hashing (25分)

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  9. PAT (Advanced Level) 1078. Hashing (25)

    二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include< ...

随机推荐

  1. NLP(二十四)利用ALBERT实现命名实体识别

      本文将会介绍如何利用ALBERT来实现命名实体识别.如果有对命名实体识别不清楚的读者,请参考笔者的文章NLP入门(四)命名实体识别(NER) .   本文的项目结构如下:   其中,albert_ ...

  2. 测试 - 某网站ACCESS数据库注入漏洞

    元宵节 团团圆圆总少不了一篇文  测试是否有注入 测试数据库类型 后面不用注释猜到可能是access 验证一下 这里说一下MySQL和ACCESS以及MSSQL的判断语句 MySQL:and len ...

  3. 小巧开源的 baresip VOIP 项目

    Baresip is a modular SIP User-Agent with audio and video support https://github.com/alfredh/baresip ...

  4. 超强图文|并发编程【等待/通知机制】就是这个feel~

    你有一个思想,我有一个思想,我们交换后,一个人就有两个思想 If you can NOT explain it simply, you do NOT understand it well enough ...

  5. [日志分析]Graylog2采集mysql慢日志

    之前聊了一下graylog如何采集nginx日志,为此我介绍了两种采集方法(主动和被动),让大家对graylog日志采集有了一个大致的了解. 从日志收集这个角度,graylog提供了多样性和灵活性,大 ...

  6. (转)协议森林05 我尽力 (IP协议详解)

    协议森林05 我尽力 (IP协议详解) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! IPv4与IPv6头部的对比 我们已经在I ...

  7. (转)协议森林03 IP接力赛 (IP, ARP, RIP和BGP协议)

    协议森林03 IP接力赛 (IP, ARP, RIP和BGP协议) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 网络层(net ...

  8. EPX-Studio脚本调用

    procedure TF408017792.Button1Click(Sender: TObject); var NEPX: IExcelPanelXDisp; begin NEPX := this. ...

  9. 在Centos系统中基于PowerDNS和Poweradmin自建域名解析服务器替代DnsPod

    本文讲述了我在Centos 7系统(其他版本的Centos未尝试)中基于PowerDNS和poweradmin自建域名解析服务器替代DnsPod的过程.通过本文所述方法,可以建立权威域名解析服务器的m ...

  10. IOS抓包工具Stream——让移动端的抓包变得轻而易举

    有一天下晚班回家,在地铁上的时候,开发发来信息说,能不能把之前创建的bug再抓包看下数据.顿时心里就想,在地铁上,我上哪抓包去.之后百度了下,发现ios有一款非常实用的抓包工具,大家可以上App St ...