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. javascript-单例模式

    单例模式笔记   也称为单体模式,只允许实例化一次的对象类   用法:      1.命名空间:用一个对象来规划一个命名空间,井井有条的管理对象上的属性和方法      2.静态变量管理:让创建的函数 ...

  2. Spring,hibernate,struts的面试笔试题及答案

    Hibernate工作原理及为什么要用?  原理:  1.读取并解析配置文件  2.读取并解析映射信息,创建SessionFactory  3.打开Sesssion  4.创建事务Transation ...

  3. C++/CLI——读书笔记《Visual C++/CLI从入门到精通》 第Ⅱ部分

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

  4. ORA-01858: 在要求输入数字处找到非数字字符

    数据库   date  字段问题  insert into  WK_RE_LE  (DACL_FILE_ID,DACL_GROUP_ID,BDCDYH,DACL_LENGTH,ISVALID,DACL ...

  5. my_strcat()

    char* my_strcat(char* S1,const char* S2){ //严格符合strcat()的接口形式,需要的S1空间是两个字符串空间总和-1. int i=0,j=0; whil ...

  6. 【JAVA 小结】Java关于类与对象的代码

    分别建立2个类class works 和 Person import java.io.*; public class works { public static void main(String[] ...

  7. [转]ASP.NET MVC 3 Razor + jqGrid 示例

    本文转自:http://www.cnblogs.com/think8848/archive/2011/07/15/2107828.html 前些天写了一篇有关jqGrid的文章,因为是公司项目的原因一 ...

  8. 周五了啦啦啦啦-LAMP+PHP‘s OOP

    hi 周五咯~~ 1.LAMP配置完结篇 五.LAMP配置环境优化 5.4 虚拟主机工作原理 apache的虚拟主机.virtual-host 用不同的域名访问不同的目录——手动模拟dns 修改hos ...

  9. Stanford机器学习笔记-10. 降维(Dimensionality Reduction)

    10. Dimensionality Reduction Content  10. Dimensionality Reduction 10.1 Motivation 10.1.1 Motivation ...

  10. Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)【转】【修改】

    一.基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成 ...