题目地址

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(" ");
}
}

  

PTA 11-散列2 Hashing (25分)的更多相关文章

  1. PTA 字符串关键字的散列映射(25 分)

    7-17 字符串关键字的散列映射(25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位:再用除留余 ...

  2. PTA 逆散列问题 (30 分)(贪心)

    题目链接:https://pintia.cn/problem-sets/1107178288721649664/problems/1107178432099737614 题目大意: 给定长度为 N 的 ...

  3. 11-散列2 Hashing (25 分)

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

  4. PTA甲级1094 The Largest Generation (25分)

    PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...

  5. pat09-散列1. Hashing (25)

    09-散列1. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue The task of ...

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

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

  7. PTA 10-排序5 PAT Judge (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/677 5-15 PAT Judge   (25分) The ranklist of PA ...

  8. PTA 05-树7 堆中的路径 (25分)

    题目地址 https://pta.patest.cn/pta/test/15/exam/4/question/713 5-5 堆中的路径   (25分) 将一系列给定数字插入一个初始为空的小顶堆H[] ...

  9. PTA 04-树5 Root of AVL Tree (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/668 5-6 Root of AVL Tree   (25分) An AVL tree ...

随机推荐

  1. 融云SDK触达用户数破20亿 王者风范双倍展现

    11月1日,融云SDK触达用户数突破20亿,业务增长速度及用户覆盖量再创即时通讯云领域新高.自去年11月10日公布SDK触达用户数破10亿以来,融云仅用了一年时间,便取得了触达用户数翻倍的成绩,迅猛的 ...

  2. mysql 速度检索

    授权GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'127.0.0.1' IDENTIFIED BY 'zabbixpwd' WITH GRANT OPTI ...

  3. 使用ImageList组件制作动画图片

    实现效果: 知识运用: Timer组件的Enabled属性  Tick事件 PictureBox控件的Image属性 ImageList组件的Images属性 实现代码: private void F ...

  4. VMware9虚拟机安装MAC OS X Mountain Lion 10.8.2详细图文教程

    VMware虚拟机安装Mac OS X Mountain Lion 10.8.2所需文件:1.Vmware 9.01版下载:点击进入2.Vmware 9.01版汉化文件:点击进入3.VMware Wo ...

  5. iOS 资源大全整理

    这是个精心编排的列表,它包含了优秀的 iOS 框架.库.教程.XCode 插件.组件等等. 这个列表分为以下几个部分:框架( Frameworks ).组件( Components ).测试( Tes ...

  6. poj3335 Rotating Scoreboard

    题目描述: vjudge POJ 题解: 半平面交判核的存在性. 重点在于一个点的核也算核. 这样的话普通的求多边形的版本就要加一个特判. 就是把剩下的一个节点暴力带回所有直线重判,这时判叉积是否$\ ...

  7. 【上下界网络流 费用流】bzoj2055: 80人环游世界

    EK费用流居然写错了…… Description     想必大家都看过成龙大哥的<80天环游世界>,里面的紧张刺激的打斗场面一定给你留下了深刻的印象.现在就有这么     一个80人的团 ...

  8. Ubuntu 下安装mysqlclient报错

    pip3 install mysqlclient 报错信息 问题描述: Complete output from command python setup.py egg_info: /bin/sh: ...

  9. 让Web站点崩溃最常见的七大原因

    转载自: https://blog.csdn.net/u012981511/article/details/53503242 1. 磁盘已满 导致系统无法正常运行的最可能的原因是磁盘已满.一个好的网络 ...

  10. linux 上安装配置l2tp的客户端

    有些时候我们外网linux服务器需要访问内网的服务器,这时候就需要在外网服务器上配置l2tp的客户端,连接到VPN访问内网服务器. 安装: yum -y install xl2tpd ppp 安装成功 ...