This page is still under construction.

Parts of this page are still being built. What you see may change.

Perfect Hash

Time limit1sMemory limit128 MB

Summary
For each line of up to 13 short words, find the smallest positive integer C so the hash floor(C/w) mod n is collision free, and print C after echoing the input line.
Level

Hard8 of 10

Topics
Hash map, Math, Brute force, Implementation
Solved
No attempts yet

Problem

Perfect Software, Inc. has a government contract to scan text flowing through a high-speed network for certain words. The system checks each word against a group of small perfect hash tables, and your task is to build the perfect hash function for each table.

A perfect hash function maps every input directly into a fully occupied table (no collisions and no empty slots). The hash function has the form ⌊C/w⌋ mod n\lfloor C / w \rfloor \bmod n, where:

  • CC is a positive integer you must discover,
  • ww is the integer representation of an input word, and
  • nn is the length of the table (the number of words in the list).

CC must be as small as possible. Here ⌊R⌋\lfloor R \rfloor is the floor of RR: the largest integer that is ≤R\le R.

Finding CC. Let W={w1,w2,…,wn}W = \lbrace w_1, w_2, \ldots, w_n \rbrace be the word values, sorted so that w1<w2<⋯<wnw_1 < w_2 < \cdots < w_n. Find the smallest positive integer CC such that

⌊Cwi⌋ mod n≠⌊Cwj⌋ mod nfor all 1≤i<j≤n.\left\lfloor \frac{C}{w_i} \right\rfloor \bmod n \neq \left\lfloor \frac{C}{w_j} \right\rfloor \bmod n \quad \text{for all } 1 \le i < j \le n.

The smallest such CC is always a multiple of at least one element of WW.

A useful observation: if ⌊Cwi⌋ mod n=⌊Cwj⌋ mod n\left\lfloor \frac{C}{w_i} \right\rfloor \bmod n = \left\lfloor \frac{C}{w_j} \right\rfloor \bmod n for some pair i≠ji \neq j (a collision), then neither of those two floor values can change until CC reaches at least

min⁡((⌊Cwi⌋+1)⋅wi,  (⌊Cwj⌋+1)⋅wj),\min\left( \left( \left\lfloor \frac{C}{w_i} \right\rfloor + 1 \right) \cdot w_i, \; \left( \left\lfloor \frac{C}{w_j} \right\rfloor + 1 \right) \cdot w_j \right),

so no smaller CC can resolve that collision. Because every collision must be resolved, it is efficient to advance CC to the largest of these thresholds over all current collisions and test again.

Encoding a word. Convert each word to a number by processing its letters from left to right. Treat 'a' as 1, 'b' as 2, …\ldots, 'z' as 26, using 5 bits per letter (shift left by 5, i.e. multiply by 32, before adding the next letter). Thus 'a' =1= 1 and 'bz' =(2⋅32)+26=90= (2 \cdot 32) + 26 = 90.

Input

The input is a series of word lists, one per line, until end-of-file. Each line has between two and thirteen words, each at most five lowercase letters, separated by one or more spaces. Every line contains at least one single-letter word.

Output

For each word list, print the input line, then on the next line print the value of CC for that list's hash function. Print a blank line between the answers for consecutive lists. CC always fits in a signed 32-bit integer.

Examples3

  1. Example 1

    Input
    this is a test of some words to try out
    a bee see dee
    the of and to a in that is i it with for as
    
    Expected output
    this is a test of some words to try out
    17247663
    
    a bee see dee
    4427
    
    the of and to a in that is i it with for as
    667241
    
  2. Example 2

    Input
    a b
    
    Expected output
    a b
    1
    
  3. Example 3

    Input
    a b c
    
    Expected output
    a b c
    2