11-散列2 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%TSize where TSizeis 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 (≤10​4​​) and N (≤MSize) 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 -

平法探测法的训练, 很基础

#include <stdio.h>
#include <stdlib.h>
#include <math.h> #define MAXTABLESIZE 100000 int IsPrime(int N);
int Find( int Size, int Key, int* Cells); int main(){
int M, N;
int Tmp, i;
int tmp;
freopen( "C:\\in.txt", "r", stdin );
scanf("%d %d", &M, &N);
if( M>)
while(!IsPrime(M)) M++;
else M = ;
int* Cells = (int*)malloc(M*sizeof(int));
for(i = ; i<M; i++) Cells[i] = ;
for(i = ; i<N; i++){
scanf("%d", &Tmp);
tmp = Find( M, Tmp, Cells);
if(tmp>=)
printf("%d", tmp);
else printf("%c", '-');
if(i!= N-) printf(" ");
}
printf("\n");
free(Cells);
return ;
} int Find( int Size, int Key, int* Cells){
int CurrentPos, NewPos;
int CNum = ;
NewPos = Key%Size;
if( !Cells[NewPos] )
Cells[NewPos] = ;
else
{
for( CNum = ; CNum<Size; CNum++ ){
CurrentPos = (NewPos+CNum*CNum)%Size;
if( !Cells[CurrentPos] ){
Cells[CurrentPos] = ;
NewPos = CurrentPos;
break;
}
}
if(CNum>=Size) NewPos = -;
}
return NewPos;
} int IsPrime(int N){
int p;
for( p = ; p<=(int)sqrt(N); p++ ) {
if(N%p == ) {
p = ;
break;
}
}
return p;
}

PAT1078 Hashing的更多相关文章

  1. pat1078. Hashing (25)

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

  2. PAT-1078 Hashing (散列表 二次探测法)

    1078. Hashing The task of this problem is simple: insert a sequence of distinct positive integers in ...

  3. PAT1078 Hashing 坑爹

    思路:用筛法给素数打表,二次探测法(只需要增加的)–如果的位置被占,那么就依次探测. 注意:如果输入的,这也不是素数:如果,你需要打表的范围就更大了,因为不是素数. AC代码 #include < ...

  4. [Algorithm] 局部敏感哈希算法(Locality Sensitive Hashing)

    局部敏感哈希(Locality Sensitive Hashing,LSH)算法是我在前一段时间找工作时接触到的一种衡量文本相似度的算法.局部敏感哈希是近似最近邻搜索算法中最流行的一种,它有坚实的理论 ...

  5. Consistent hashing —— 一致性哈希

    原文地址:http://www.codeproject.com/Articles/56138/Consistent-hashing 基于BSD License What is libconhash l ...

  6. 一致性 hash 算法( consistent hashing )a

    一致性 hash 算法( consistent hashing ) 张亮 consistent hashing 算法早在 1997 年就在论文 Consistent hashing and rando ...

  7. PTA Hashing

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

  8. Feature hashing相关 - 1

    考虑典型的文本分类,一个经典的方法就是     分词,扫描所有特征,建立特征词典 重新扫描所有特征,利用特征词典将特征映射到特征空间编号 得到特征向量 学习参数 w 存储学习参数 w , 存储特征映射 ...

  9. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 H. Hashing

    H. Hashing time limit per test 1 second memory limit per test 512 megabytes input standard input out ...

随机推荐

  1. WPF学习之路(三) 属性与依赖

    类型是DependencyProperty的属性是依赖属性 依赖属性不同于普通的.Net属性,类似于一个计算过程,根据依赖的值得到最终值. 为什么引入依赖属性: MSDN原文 One of the p ...

  2. SQL Server 2008 R2——分组取前几名

    =================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载  请通过右 ...

  3. su认证失败&文件夹里打开终端的方法&atom安装

    很久没用笔记本上的ubuntu,用不顺手,比在公司调教了半年多的电脑差远了.一步一步来.先解决最不顺手的三件事 1.su认证失败. 新安装的ubuntu系统是无法切换到root账户的,得做一番修改 s ...

  4. Linux工具之man手册彩色页设置

    说明: 对于我们开发人员或者运维工程师来说,经常要查询某个系统命令或者C函数接口的使用方法,最好的最专业的资料就是man手册,通过一些设置可以让man手册页面显示适当颜色,方便阅读,增强美观性. 设置 ...

  5. 王总QQ聊天对话

    在吗? LeeYu 2015/11/10 9:37:11 在的,王总 王宁 2015/11/10 9:37:29 李伟,你今天把设备送到H3C吧. LeeYu 2015/11/10 9:37:43 行 ...

  6. mysql全量备份脚本

    #!/bin/bash# Program# use mysqldump to Fully backup mysql data per week!# History# PathBakDir=/data/ ...

  7. x01.os.5: DOS 功能调用

    DOS 功能调用(INT 21)-------------------------------AH = 0-2E 适用 DOS 1.0 以上版本AH = 2F-57 适用 DOS 2.0 以上版本AH ...

  8. 构建 ARM Linux 4.7.3 嵌入式开发环境 —— U-BOOT 引导 Kernel

    经过若干天的反复测试,搜索.终于成功利用 Qemu 在 u-boot 下引导 ARM Linux 4.7.3 内核.如下详细解释整个构建过程. 准备环境 运行环境:Ubuntu 16.04 需要的虚拟 ...

  9. [转]ASP.NET Web API(三):安全验证之使用摘要认证(digest authentication)

    本文转自:http://www.cnblogs.com/parry/p/ASPNET_MVC_Web_API_digest_authentication.html 在前一篇文章中,主要讨论了使用HTT ...

  10. [麦先生]Laravel框架实现发送短信验证

    今天在做到用户注册和个人中心的安全管理时,我借助实现第三方短信平台在Laravel框架中进行手机验证的设置;  由于我们做的是一个为客户提供医疗咨询和保健品网站,所以对客户个人隐私的保护显得尤为重要, ...