pat09-Hash 1. Hashing (25)

Source: Internet
Author: User

09-Hash 1. Hashing (25) time limit MS Memory limit 65536 KB code length limit 8000 B procedure StandardAuthor Chen, Yue

The task of this problem are simple:insert a sequence of distinct positive integers into a hash table, and output the Posi tions of the input numbers. The hash function is defined to being "H (key) = key% tsize" where tsize is the maximum size of the hash table. Quadratic probing (with positive increments only) are used to solve the collisions.

Note The table size is better to being prime. If the maximum size given by the user are not prime, you must re-define the table size to be the smallest prime number whic H is larger than the size given by the user.

Input Specification:

Each input file contains the one test case. For each case, the first line contains the positive numbers:msize (<=104) and N (<=msize) which is the User-define d table size and the number of input numbers, respectively. Then N distinct positive integers is given in the next line. All the numbers in a line is separated by a space.

Output Specification:

For each test case, print the corresponding positions (index starts from 0) of the of the input numbers on one line. All the numbers in a line is separated by a space, and there must is no extra space at the end of the line. In case it was impossible to insert the number, print "-" instead.

Sample Input:
4 410 6) 4 15
Sample Output:
0 1 4-

Submit Code

Watch the boundary test!

1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <queue>6#include <vector>7 using namespacestd;8 BOOLh[10005];9 intGetnextprime (intnum) {Ten     inti,j; One     if(num==1){//Note the border A         return 2; -     } -     if(num==2){//Note the border the         return 2; -     } -      for(I=num;; i++){ -         if(i%2==0){ +             Continue; -         } +          for(j=3; j*j<=i;j+=2){ A             if(i%j==0){ at                  Break; -             } -         } -         if(j*j>i) { -             returni; -         } in     } - } to intMain () { +     //freopen ("D:\\input.txt", "R", stdin); -     intMsize,n; thescanf"%d%d",&msize,&n); *     inti,num,j; $memset (H,false,sizeof(h));Panax NotoginsengMsize=Getnextprime (msize); -queue<int>Q; the      for(i=0; i<n;i++){ +scanf"%d",&num); Anum=num%msize; the         if(!H[num]) { +h[num]=true; - Q.push (num); $         } $         Else{ -             intNext; -              for(j=1; j<=msize/2; j + +){ thenext= (num+j*j)%msize; -                 if(!H[next]) {Wuyih[next]=true; the Q.push (next); -                      Break; Wu                 } -             } About             if(j>msize/2){ $Q.push (-1); -             } -         } -     } A     intNow ; +     if(!Q.empty ()) { thenow=Q.front (); - Q.pop (); $         if(now==-1){ theprintf"-"); the         } the         Else{ theprintf"%d", now); -         } in     } the      while(!Q.empty ()) { thenow=Q.front (); About Q.pop (); the         if(now==-1){ theprintf" -"); the         } +         Else{ -printf"%d", now); the         }Bayi     } theprintf"\ n"); the     return 0; -}

pat09-Hash 1. Hashing (25)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.