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 }

 

转载于:https://www.cnblogs.com/Deribs4/p/4729919.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值