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 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快 ...
随机推荐
- php简单文件上传类
<?php header("Content-Type:text/html; charset=utf-8"); if($_POST['submit']){ $upfiles = ...
- SQL Server 2012 连接到数据库引擎
第 1 课:连接到数据库引擎 https://msdn.microsoft.com/zh-cn/library/ms345332(v=sql.110).aspx 本课将介绍主要的工具以及如何连接并 ...
- Yii CActiveForm
http://blog.sina.com.cn/s/blog_685213e70101mo4i.html 文档: http://www.yiiframework.com/doc/api/1.1/CAc ...
- 【Lucene3.6.2入门系列】第04节_中文分词器
package com.jadyer.lucene; import java.io.IOException; import java.io.StringReader; import org.apach ...
- linux下tar压缩/解压的使用(tar) 压缩/解压
压缩: tar -zcvf 压缩后文件名.tar.gz 被压缩文件 解压: tar -zxvf 被解压文件 具体的可以在linux环境下 用 tar --help 查看详细说明格式:ta ...
- [.NET MVC进阶系列03] Views 视图基础
[注:此文对应Chapter 3:Views] 一.View的功能: 1.View用来呈现页面UI,通过Controller来指定View: 要注意的是,MVC和以前基于文件的Web应用不同,URL指 ...
- 算法的时间复杂度(大O表示法)
定义:如果一个问题的规模是n,解这一问题的某一算法所需要的时间为T(n),它是n的某一函数 T(n)称为这一算法的“时间复杂性”. 当输入量n逐渐加大时,时间复杂性的极限情形称为算法的“渐近时间复杂性 ...
- servlet规范
Servlet规范 一个最基本的 Java Web 项目所需的 jar 包只需要一个 servlet-api.jar ,这个 jar 包中的类大部分都是接口,还有一些工具类,共有 2 个包,分别是 j ...
- ViewPager 滑动页(二)
需求:滑动展示页,能够使用本地数据,及获取服务器数据进行刷新操作,并实现页面自动切换: 效果图: 实现分析: 1.目录结构: 代码实现: 1.PosterView.java package com.j ...
- ASDL + WN725N 配置无线AP
1. ASDL 正常拨号上网 2. 安装TP-LINK无线客户端应用程序 打开之后选择模拟AP 如下图设置----应用 3. 本地连接----属性----高级 如下图设置 4. 宽带连接--- ...