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. git技巧记录--子模块删除方法

    把子模块推进去了,删掉吧(将子模块删除,然后提交推送),删除子模块步骤: 1.在Platform.Web库下,右键->Git Bash,进入git命令行窗口,输入:git rm –-cached ...

  2. c# 注册全局热键

    //引入系统API [DllImport("user32.dll")] static extern bool RegisterHotKey(IntPtr hWnd, int id, ...

  3. 烂泥:使用nginx利用虚拟主机搭建WordPress博客

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近开始打算学习nginx web服务器,既然是学习还是以实用为目的的.我们在此以搭建WordPress博客为例. 搭建WordPress博客,我们需要 ...

  4. CentOS 6.3下配置LVM(逻辑卷管理)

    一.简介 LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵 ...

  5. Centos 7 ASP.NET Core 1.0 Docker部署

    先决条件 64位,内核3.10以上,查看当前的内核版本,打开一个终端使用uname -r显示您的内核版本             安装 sudo yum update     sudo tee /et ...

  6. Ubuntu13.04安装历险记--Mono,Nginx,Asp.Net一个都不能少

    ----Ubuntu13.04安装历险记--新人新手新作------------------------------------------------- 注:以下操作均省略权限获取操作,如有需要,请 ...

  7. linux下motion摄像头监控编译与配置

    利用linxu下的开源的motion搭建嵌入式视频动态监控系统 所谓移动图像监测,简单来说就是利用摄像头定点监测某个区域,当有移动物体经过时,摄像头便自动抓拍(要监测多大物体.按拍照速率都是可调的), ...

  8. [转]simple sample to create and use widget for nopcommerce

    本文转自:http://badpaybad.info/simple-sample-to-create-and-use-widget-for-nopcommerce Here is very simpl ...

  9. cuda中thread id

    //////////////////////////////////////////////////////////////////////////// // // Copyright 1993-20 ...

  10. Codeforces 549G Happy Line[问题转换 sort]

    G. Happy Line time limit per test 1 second memory limit per test 256 megabytes input standard input ...