1078. Hashing (25) "Hash + Probe"--pat (Advanced level) practise

Source: Internet
Author: User

Topic Information 1078. Hashing (+)

Time limit (MS)
Memory Limit 65536 KB
Code length limit 16000 B
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 (<=10^4) and N (<=msize) which is the User-defin Ed 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 4
10 6 4 15
Sample Output:
0 1 4-

Thinking of solving problems

Title default you have mastered square two probes, there are many pits forcing,,,,

AC Code
#include <cstdio>#include <cmath>BOOLa[20005];BOOLPrimeintA) {if(1= = a)return false;if(2= = A | |3= = a)return true;intT = (int)sqrt(a); for(inti =2; I <= t; ++i) {if(a%i = =0){return false; }    }return true;}intMain () {intMS, N, TS, T, I, J;scanf("%d%d", &ms, &n); ts = ms; while(!prime (TS))    {++ts; } for(i =0; I < n; ++i) {scanf("%d", &t); for(j =0; J < N; ++J) {if(!a[(t + j*j)% ts]) {a[(t + j*j)% ts] =true;printf("%s%d"I?" ":"", (t + j*j)% ts); Break; }        }intloc = t% ts;if(j = = N) {printf("%s-"I?" ":""); }    }return 0;}

1078. Hashing (25) "Hash + Probe"--pat (Advanced level) practise

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.