pat09-散列1. Hashing (25)

09-散列1. Hashing (25)

时间限制

100 ms

内存限制

65536 kB

代码长度限制

8000 B

判题程序

Standard

作者

CHEN, Yue

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 TSize 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: MSize (<=104) 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 -


提交代码

注意边界测试!

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<queue>
 6 #include<vector>
 7 using namespace std;
 8 bool h[10005];
 9 int GetNextPrime(int num){
10     int i,j;
11     if(num==1){//注意边界
12         return 2;
13     }
14     if(num==2){//注意边界
15         return 2;
16     }
17     for(i=num;;i++){
18         if(i%2==0){
19             continue;
20         }
21         for(j=3;j*j<=i;j+=2){
22             if(i%j==0){
23                 break;
24             }
25         }
26         if(j*j>i){
27             return i;
28         }
29     }
30 }
31 int main(){
32     //freopen("D:\\INPUT.txt","r",stdin);
33     int msize,n;
34     scanf("%d %d",&msize,&n);
35     int i,num,j;
36     memset(h,false,sizeof(h));
37     msize=GetNextPrime(msize);
38     queue<int> q;
39     for(i=0;i<n;i++){
40         scanf("%d",&num);
41         num=num%msize;
42         if(!h[num]){
43             h[num]=true;
44             q.push(num);
45         }
46         else{
47             int next;
48             for(j=1;j<=msize/2;j++){
49                 next=(num+j*j)%msize;
50                 if(!h[next]){
51                     h[next]=true;
52                     q.push(next);
53                     break;
54                 }
55             }
56             if(j>msize/2){
57                 q.push(-1);
58             }
59         }
60     }
61     int now;
62     if(!q.empty()){
63         now=q.front();
64         q.pop();
65         if(now==-1){
66             printf("-");
67         }
68         else{
69             printf("%d",now);
70         }
71     }
72     while(!q.empty()){
73         now=q.front();
74         q.pop();
75         if(now==-1){
76             printf(" -");
77         }
78         else{
79             printf(" %d",now);
80         }
81     }
82     printf("\n");
83     return 0;
84 }
时间: 2024-08-28 22:46:21

pat09-散列1. Hashing (25)的相关文章

PTA 字符串关键字的散列映射(25 分)

7-17 字符串关键字的散列映射(25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数H(Key)将关键字Key中的最后3个字符映射为整数,每个字符占5位:再用除留余数法将整数映射到长度为P的散列表中.例如将字符串AZDEG插入长度为1009的散列表中,我们首先将26个大写英文字母顺序映射到整数0~25:再通过移位将其映射为3×32?2??+4×32+6=3206:然后根据表长得到,即是该字符串的散列映射位置. 发生冲突时请用平方探测法解决. 输入格式: 输入第

数据结构--散列排序--散列表

散列表 散列查找,我们又回到了查找, 编译的时候,涉及变量及属性的管理: 插入:新变量的定义 查找:变量的引用 实际上是动态查找问题,查找树AVL树. 两个变量名(字符串)比较效率不高.字符串的比较要一个一个的比下去,时间会比较长, 是否可以把字符串转换成数字,再处理,就快多了.就是散列查找的思想. 已知的查找方法: 顺序查找                                          O(N) 二分查找(静态查找,不适合动态查找)   O(log2N) 二叉搜索数    

数据结构学习笔记07散列查找

1.散列表(Hash) 查找的本质: 已知对象找位置. 有序安排对象:全序.半序 直接“算出”对象位置:散列 时间复杂度几乎是常量:O(1),即查找时间与问题规模无关 散列查找法的两项基本工作: 计算位置:构造散列函数确定关键词存储位置: 解决冲突:应用某种策略解决多个关键词位置相同的问题 散列(Hashing) 的基本思想是: ①以关键字key为自变量,通过一个确定的函数 h(散列函数),计算出对应的函数值h(key),作为数据对象的存储地址. ②可能不同的关键字会映射到同一个散列地址上,即h

散列表之完美散列

散列表之完美散列 完美散列perfect hashing 两级散列法 gperf工具来自动生成完美散列函数 gperf的安装 gperf关键字文件的书写格式 gperf应用举例 注意 本文中的所有代码你都可以在这里: https://github.com/qeesung/algorithm/tree/master/chapter11/11-5/gperf(这里会及时更新) 或者是这里: http://download.csdn.net/detail/ii1245712564/8936303 找到

PTA数据结构与算法题目集(中文) 7-43字符串关键字的散列映射 (25 分)

PTA数据结构与算法题目集(中文)  7-43字符串关键字的散列映射 (25 分) 7-43 字符串关键字的散列映射 (25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义的散列函数(将关键字Key中的最后3个字符映射为整数,每个字符占5位:再用除留余数法将整数映射到长度为P的散列表中.例如将字符串AZDEG插入长度为1009的散列表中,我们首先将26个大写英文字母顺序映射到整数0~25:再通过移位将其映射为3:然后根据表长得到,即是该字符串的散列映射位置. 发生冲突时请

转载:散列冲突的解决策略

冲突解决的策略 尽管散列函数的目标是使得冲突最少,但实际上冲突是无法避免的.因此,我们必须研究冲突解决策略.冲突解决技术可以分为两类:开散列方法( open hashing,也称为拉链法,separate chaining )和闭散列方法( closed hashing,也称为开地址方法,open addressing ).这两种方法的不同之处在于:开散列法把发生冲突的关键码存储在散列表主表之外,而闭散列法把发生冲突的关键码存储在表中另一个槽内. 开散列方法: 1.拉链法 开散列方法的一种简单形

数据结构与算法分析java——散列

1. 散列的概念 散列方法的主要思想是根据结点的关键码值来确定其存储地址:以关键码值K为自变量,通过一定的函数关系h(K)(称为散列函数),计算出对应的函数值来,把这个值解释为结点的存储地址,将结点存入到此存储单元中.检索时,用同样的方法计算地址,然后到相应的单元里去取要找的结点.通过散列方法可以对结点进行快速检索.散列(hash,也称“哈希”)是一种重要的存储方式,也是一种常见的检索方法. 按散列存储方式构造的存储结构称为散列表(hash table).散列表中的一个位置称为槽(slot).散

散列(C++实现)

散列的构成:散列函数,散列表的存储方式,散列表的冲突解决方法. 1.散列函数 较常用的散列函数有除留余数法,数字分析法,平方取中法,折叠法. 2.散列表的存储方式 闭散列法(开地址法),用数组存储:开散列法(链地址法),用邻接链表存储. 3.散列表的冲突解决方法 主要是针对闭散列中关键码位置冲突的问题,常用的方法有线性探查法,二次探查法,双散列法. 性能分析:在存储方式中,开散列法优于闭散列法:在散列函数中,除留余数法最优. 实现代码: 1 #include<iostream> 2 using

没事写个散列玩~

感觉散列的查找性能真心不错,如果使用普通线性结构查找,平均时间是n/2.而刚才用实验,256大小的散列,存储128个数据,平均时间为2次以内.感觉真心NB 1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <cstdlib> 5 #include <ctime> 6 7 using namespace std; 8 typedef bool BOOL;