题目地址

https://pta.patest.cn/pta/test/16/exam/4/question/679

5-17 Hashing   (25分)

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 \% TSizeH(key)=key%TSizewhere TSizeTSize 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: MSizeMSize(\le 10^4≤10​4​​) and NN (\le MSize≤MSize) which are the user-defined table size and the number of input numbers, respectively. Then NN 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 -

难点在于计算什么情况下是实在插不进去的
/*
评测结果
时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2017-07-07 18:23 答案正确 25 5-17 gcc 31 1
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 12/12 2 1
测试点2 答案正确 3/3 2 1
测试点3 答案正确 5/5 1 1
测试点4 答案正确 5/5 31 1 此题是用平方探测法解决冲突。
为了解决不可插入问题,用了一个链表来存尝试过的下标。如果之前尝试过不成功的下标又出现一次,说明实在是插不进去了。
*/
#include<stdio.h>
#include<stdlib.h>
#define DBG printf
#define MAXN 20000
#define EMPTY -1
typedef struct Collisions* pCollisions; //冲突结点的结构体它的指针类型
struct Collisions{
int value;
pCollisions next;
}; int gHashTable[MAXN]; void InitHashTable() //把表初始化一下
{
int i;
for(i=0;i<MAXN;i++)
gHashTable[i]=EMPTY;
} int SearchCollisionsNode(pCollisions P,int x) //在链表中搜索有没有曾经访问过x下标
{
while(P!=NULL)
{
if(P->value==x)
return 1;
else P=P->next;
}
return 0;
} pCollisions CreateCollisionNode(int N) //创建一个储存着冲突数据下标的结点
{
pCollisions P=malloc(sizeof(struct Collisions));
P->next=NULL;
P->value=N;
return P;
} void DestroyCollision(pCollisions P) //回收内存
{
if(P==NULL)
return;
else DestroyCollision(P->next);
free(P);
} int FindNextPrime(int n) //找个比n大的质数
{
if(n==1) //在这里有个坑,第二个测试点卡表大小填1的时候。
return 2;
int i,isPrime;
while(1)
{
isPrime=1;
for(i=2;i<n;i++)
if(n%i==0)
{
isPrime=0;
break;
}
if(isPrime)
return n;
else n++;
}
} void Insert(int x,int M) //插入hash表
{
struct Collisions head;
head.next=NULL;
int idx,collCount=0;
pCollisions P;
while(gHashTable[idx=(x+collCount*collCount)%M]!=EMPTY)
{
if(SearchCollisionsNode(head.next,idx)==1)
{
printf("-");
DestroyCollision(head.next);
return;
}
else
{
collCount++;
P=CreateCollisionNode(idx);
P->next=head.next;
head.next=P;
}
}
gHashTable[idx]=x;
printf("%d",idx);
DestroyCollision(head.next);
} int main()
{
int i,M,N,temp;
scanf("%d %d",&M,&N);
M=FindNextPrime(M);
InitHashTable(); //容易漏的地方,初始化表
// DBG("got Prime M=%d\n",M);
for(i=0;i<N;i++)
{
scanf("%d",&temp);
Insert(temp,M);
if(i!=N-1)
printf(" ");
}
}

  

最新文章

  1. Liunx下查看服务器硬件信息
  2. Android 6.0 新特性
  3. 第十一篇 SQL Server代理维护计划
  4. IOS缓存之NSCache缓存
  5. IE6/IE7中li底部4px的Bug
  6. unigui下载文件
  7. javascript学习教程之---如何从一个tab切换到banner幻灯片的转换
  8. jquery easyui二次开发总结(二)
  9. windows phone因为墓碑化导致“正在恢复”的分析
  10. ARM简介(科普文)
  11. Redis 数据结构与内存管理策略(上)
  12. 如何用JavaScript制作循环图形
  13. 11、Libgdx的音频
  14. APK改之理 手游修改改编安卓程序工具安装使用教程
  15. 解决win10隔几分钟自动黑屏睡眠的方法
  16. Robot Framework 1
  17. [转]NMON服务器监控、指标说明
  18. python 对shell 命令的 执行 逻辑 在一台机器上执行另一台机器的命令; 跨节点 执行命令
  19. webbrowser 常用方法(C#)
  20. 零基础学python-5.4 数字精度与复数

热门文章

  1. SAP OData编程指南
  2. 如果不需要,建议移除net standard类库中的Microsoft.NETCore.Portable.Compatibility
  3. 小常识:变量的修饰符和DEMO
  4. UVA 1613 K-Graph Oddity K度图着色 (构造)
  5. python 列表 字典转json
  6. oracle 数据导到 sql server
  7. java基础—GUI编程(二)
  8. 用Windows Native API枚举所有句柄及查找文件句柄对应文件名的方法
  9. RabbitMQ 学习资料
  10. Tomcat:使用startup.bat启动tomcat遇到报错