D
D
ddddd tttt2020-08-18 03:19:43
Algorithms
ddddd tttt, 2020-08-18 03:19:43

How to get a numeric hash of a given length?

There is a line consisting of Russian or any alphabet. You need to get its numeric hash of a given length. Even if this string consists of one letter or several hashes must have the same length. This is needed to sort alphabetically but through a numeric hash in order to quickly look up the first occurrence to display hints.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Demin, 2020-08-18
@includedlibrary

Take any hash function. For example, the Jenkins hash function.

uint32_t jenkins_one_at_a_time_hash(char *key, size_t len)
{
    uint32_t hash, i;
    for(hash = i = 0; i < len; ++i)
    {
        hash += key[i];
        hash += (hash << 10);
        hash ^= (hash >> 6);
    }
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);
    return hash;
}

R
Ruslan., 2020-08-18
@LaRN

This is similar to an autocomplete task. Maybe it's better to use a prefix tree instead of a hash?
This is well described for example here:
https://habr.com/ru/post/111874/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question