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. TSQL--临时表和表变量

    1. 临时表适用数据量较大的情况,因为临时表可以建立索引 2. 表变量适用于数据较小的情况,表变量只能在定义时创建约束(PRIMARY KEY/UNIQUE)从而间接建立索引 3. 临时表是事务性的, ...

  2. 【JSP】JSP基础学习记录(四)—— Servlet

    序: 众所周知JSP的基础也就是Servlet,如果单纯用Servlet类来响应用户的HTTP请求可以吗?答案是肯定的.JSP中的9个内置对象只是自动帮我们初始化的,没有他们一样可以实现web.只是工 ...

  3. MDX函数(官方顺序,带示例)

    MDX函数(官方顺序) 1.  AddCalculatedMembers (MDX) 返回通过将计算成员添加到指定集而生成的集. 语法: AddCalculatedMembers(Set_Expres ...

  4. 0011《SQL必知必会》笔记07 数据的插入、更新和删除

    1.插入完整的行或一部分:INSERT INTO 表名(列名1-n) VALUES (对应的值1-n) INSERT INTO products(prod_id, vend_id, prod_name ...

  5. PHP加密3DES报错 Call to undefined function: mcrypt_module_open() 的解决方法

    我也是PHP新手,通过w3cschool了解了一下php基本原理之后就开写了.但仍是菜鸟. 先不管3DES加密的方法对不对,方法都是网上的,在运行的时候报了个错,把小弟整死了.找来找去终于自己摸出了方 ...

  6. oracle忘记sys/system/scott用户密码了,如何重置oracle密码?

    今天用到的oracle数据库,但是发现以前设置的密码,忘记了,怎么输入都不对,所以从网上找了一下资料,解决了,然后整理分享给大家. 一.遇到的问题: 1..忘记除SYS.SYSTEM用户之外的用户的登 ...

  7. iOS同一项目多个Target的快速实现方法

    之前写过这种场景下的项目,比如类似滴滴的司机端和乘客端,学生端和教师端等等.. 最近有人问我,就整理记录如下: 1.正常新建一个项目,完成后  如下: 2.这一步就是添加一个新的target 3.添加 ...

  8. linux创建用户、设置密码、修改用户、删除用户

    创建用户.设置密码.修改用户.删除用户:useradd testuser 创建用户testuserpasswd testuser 给已创建的用户testuser设置密码说明:新创建的用户会在/home ...

  9. linux命令学习(一)—— 文件和目录管理命令

    首先需要知道的是命令提示符 [root@localhost ~] # 当前登录用户(root) 主机名(localhost) 当前所在目录(家目录) 超级用户提示符(#) 普通用户提示符($) 1.1 ...

  10. rhel7端口开放和查询

    开启端口 firewall-cmd --zone=public --add-port=80/tcp --permanent 命令含义: --zone #作用域 --add-port=80/tcp #添 ...