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≤104) 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 -
/*
* 这题就是除留余数法插入 平方探测法解决冲突(这里只有正向) 实现散列表。
*/
#include "iostream"
#include "cmath"
using namespace std;
#define MAXSIZE 20000 /* 带进去算了下这区间有素数 10006 我也是试着取得0..0.。*/
int nextPrime(int n) {
int i, j;
bool flag = true;
if (n % == )
n++;
if (n == ) /* 判断素数要注意1啊~ 在这里卡了2次- - */
return ;
for (i = n; i < MAXSIZE; i+=) {
int k = sqrt(i);
for (j = ; j<=k ; j++)
if (!(i%j))
break;
if (j > k)
return i;
}
return i;
} int val[];
void find(int a[],int m,int n) {
int i, j;
for (i = ; i < m; i++) {
for (j = ; j < n; j++) {
int pos = (val[i]%n + j*j) % n;
if (a[pos] == val[i])
{
if (i == )
cout << pos;
else
cout << " " << pos;
break;
}
}
if (j == n)
if (i == ) {
cout << "-";
}
else {
cout << " " << "-";
}
}
} void insert(int n,int m,int a[]) {
for (int i = ; i < m; i++) {
cin >> val[i];
for (int j = ; j < n; j++) {
int pos = (val[i]%n + j*j) % n;
if (!a[pos]) {
a[pos] = val[i];
break;
}
}
}
}
int main() {
int n, m;
int a[];
cin >> n >> m;
n = nextPrime(n);
for (int i = ; i < n; i++)
a[i] = ;
insert(n, m, a); /* 映射 */
find(a, m, n);
cout << endl;
return ;
}
5-17 Hashing (25分)的更多相关文章
- 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 ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- 11-散列2 Hashing (25 分)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- 【PAT甲级】1078 Hashing (25 分)(哈希表二次探测法)
题意: 输入两个正整数M和N(M<=10000,N<=M)表示哈希表的最大长度和插入的元素个数.如果M不是一个素数,把它变成大于M的最小素数,接着输入N个元素,输出它们在哈希表中的位置(从 ...
- 1078 Hashing (25分)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- 1078 Hashing (25 分)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive integ ...
- [PAT] 1143 Lowest Common Ancestor(30 分)1145 Hashing - Average Search Time(25 分)
1145 Hashing - Average Search Time(25 分)The task of this problem is simple: insert a sequence of dis ...
- PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of ...
- L2-001 紧急救援 (25 分)
L2-001 紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快 ...
随机推荐
- HeadFirst设计模式之装饰者模式
一. 1.The Decorator Pattern attaches additional responsibilities to an object dynamically.Decorators ...
- 分布式环境下的id生成方法
分布式环境下的id生成方法 前几天研究数据库分表分库的问题,其中有一个关键的地方就是生成唯一键的问题,假如数据表有1亿条数据,而且还在不断的增加,这里我们就需要考虑到分表分库,假设我们采用Hash ...
- caffe简易上手指南(一)—— 运行cifar例子
简介 caffe是一个友好.易于上手的开源深度学习平台,主要用于图像的相关处理,可以支持CNN等多种深度学习网络. 基于caffe,开发者可以方便快速地开发简单的学习网络,用于分类.定位等任务,也可以 ...
- Android 系统的四层结构
从上图中可以看出,Android 系统架构为四层结构,从上层到下层分别是应用程序层(Applications). 应用程序框架层 (Application Framework).系统运行库层(Libr ...
- Android 签名(6)编译时源码的签名
1,使用源码中的默认签名 在源码中编译一般都使用默认签名的,在某源码目录中用运行下面命令能看到签名命令. $ mm showcommands Android提供了签名的程序signapk.jar,用法 ...
- bzoj4009
这是一道神题,首先我们不难先到整体二分吧 下面的问题就是,求出对于每个水果,有多少盘子是他的子路径 直接考虑不是很容易,我们换个思路,考虑对于每个盘子,哪些水果能包含它 我们假设盘子a,b,dep[a ...
- ASP.NET中的FileUpload文件上传控件的使用
本篇文章教大家如何将客户端的图片或者文件上传到服务器: 无论是上传图片(.jpg .png .gif等等) 文档(word excel ppt 等等). 第一步:放入以下三个控件 Image控件,Fi ...
- vijos 1563 疯狂的方格取数
P1653疯狂的方格取数 Accepted 标签:天才的talent[显示标签] 背景 Due to the talent of talent123,当talent123做完NOIP考了两次的二取 ...
- acdream 小晴天老师系列——苹果大丰收(DP)
小晴天老师系列——苹果大丰收 Problem Description 小晴天的后花园有好多好多的苹果树,某天,苹果大丰收~小晴天总共摘了M个苹果,我们假设苹果之间是不可分辨的. 为了保存苹果,小晴天买 ...
- Java框架的思考
目前的JAVA 企业级开发框架,我们常用的大致包括IOC AOP MVC ORM框架 1. IOC spring是一个非常棒的ico容器,其思想非常简单,用一个集合对象如MAP 来缓存对象(对象都是单 ...