Perfect hashing

How difficult is it to find an item by key?





It depends on which data structure to use.





In a singly linked list, linear complexity.





In a sorted array or in a binary search tree, log complexity.





In a hash table, the complexity is constant. But this is at best. And at worst ... tends to be linear ...





-, ?





- . , - , - , , , -.





! , .

, " " OTUS , -.






- . , - . , !





, , - “dog”, -, , , , “” !





?

, 10.000 -. - 10.000 . “” --, , ? , , “” . 





- “(A * k + B) % N”



, A







, “” - . k



- (ulong) , N - .





.





“”,   - “”, .





. “” “”, - “”. “ ”, .





“” “”.





, , “” () “” . , . 





, 100 10.000 10.





,







- “(A * k + B) % N”



. “”, . “” () “”.





K “”



“”. “ ” A



B



K , (, - ). “” -, “”.





!





- :





  1. “” i



    - k: “(A * k + B) % N”



    .





  2. : Ai



    , Bi



    ,



    .





  3. “” : “(Ai * k + Bi) % K²







  4. “”.





( , ) “” , .





, “” -. , ( ).





, , - N = 10 . 0 10 . , .





: “” N



, “” - 1.9 * N



. : 2.9 * N = (N)



- .





, -, -- () - , .






. 15 Demo Day " " OTUS, .





, :





  • - —





  • -









  • . Bitboard












All Articles