pat1078. Hashing (25)

1078. Hashing (25)

时间限制

100 ms

内存限制

65536 kB

代码长度限制

16000 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<stack>
 3 #include<algorithm>
 4 #include<iostream>
 5 #include<stack>
 6 #include<set>
 7 #include<map>
 8 #include<cmath>
 9 using namespace std;
10 void getprime(int &msize){
11     int i=msize,j;
12     if(msize==0||msize==1||msize==2){
13         msize=2;
14         return;
15     }
16     for(;;i++){
17         if(i%2==0){
18             continue;
19         }
20         int maxl=sqrt(msize*1.0);
21         for(j=3;j<=maxl;j+=2){
22             if(i%j==0){
23                 break;
24             }
25         }
26         if(j>maxl){
27             msize=i;
28             return;
29         }
30     }
31 }
32 map<int,int> ha;
33 bool g[10005];
34 int main(){
35     //freopen("D:\\INPUT.txt","r",stdin);
36     int i,msize,n,num;
37     scanf("%d %d",&msize,&n);
38     getprime(msize);
39
40     //cout<<msize<<endl;
41
42     for(i=0;i<n;i++){
43         scanf("%d",&num);
44         ha[i]=num%msize;
45     }
46     int half=msize/2;
47     map<int,int>::iterator it=ha.begin();
48     g[it->second]=true;
49     printf("%d",it->second);
50     it++;
51     for(;it!=ha.end();it++){
52         if(!g[it->second]){
53             g[it->second]=true;
54             printf(" %d",it->second);
55         }
56         else{
57             for(i=1;i<=half;i++){
58                 if(!g[(it->second+i*i)%msize]){
59                     g[(it->second+i*i)%msize]=true;
60                     printf(" %d",(it->second+i*i)%msize);
61                     break;
62                 }
63             }
64             if(i>half){
65                 printf(" -");
66             }
67         }
68     }
69     printf("\n");
70     return 0;
71 }
时间: 2024-10-24 22:07:39

pat1078. Hashing (25)的相关文章

1078. Hashing (25)【Hash + 探测】——PAT (Advanced Level) Practise

题目信息 1078. Hashing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B 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(

PAT1078. Hashing

1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 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 fun

1078. Hashing (25)-PAT甲级真题

1078. 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 TSize is the

PAT 1078. Hashing (25)

1078. 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 TSize is the

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 fun

1078. Hashing (25)

时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 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

A1078. 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 TSize is the maximum size of th

PAT (Advanced Level) 1078. Hashing (25)

二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using namespace std; const int

5-17 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 \% TSizeH(key)=key%TSizewhere TSizeTSize is the maxi