MIPI_AdvancedC_FRTK/Lect5/55.c
2024-11-14 08:45:50 +03:00

24 lines
565 B
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <inttypes.h>
uint64_t getHash(char const *s) {
const int p = 31;
uint64_t hash = 0, p_pow = 1;
while(*s) {
/* отнимаем 'a' от кода буквы и единицу прибавляем, чтобы у строки вида 'aaaaa' хэш был ненулевой */
hash += (*s++ - 'a' + 1) * p_pow;
p_pow *= p;
}
return hash;
}
int main(void)
{
char s[100] = {0};
//~ Hello.
//~ hash = 18446744072292316036
scanf("%s",s);
printf("hash = %llu\n", getHash(s));
return 0;
}