Multiply hash:
This kind of hash function takes advantage of the non-correlation of multiplication
int Hash (char *str)
{
int seed = 131, value=0;
while (*str! = ')} {
Value = value*seed+ (*str++);
}
Return value&0x7fffffff;
}
The multiplier used here is 131, and the recommended multiplier is 1313, 13131, 131313, etc.
In addition to multiplying by a fixed number, the common ones are multiplied by a constantly changing number, such as:
int Hash (char *str)
{
int B = 378551, a = 63689;
int hash = 0;
while (*str! = ')} {
hash = hash*a+ (*str++);
A*a*b;
}
Return value&0x7fffffff;
}
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <iostream>5 using namespacestd;6 Charstr[ the];7 int_hash[ the];8 9 intHash (Char*str)Ten { One while(*str = ='0') str++;//The string of the topic to remove the preamble 0 A intSeed =131, value=0; - while(*str! =' /'){ -Value = value*seed+ (*str++); the } - returnvalue; - } - + intMain () - { + //freopen ("a.in", "R", stdin); A intN; at while(~SCANF ("%d", &N)) - { -memset (_hash,0,sizeof(_hash)); - for(intI=0; I<n; i++) - { -scanf"%s", str); in intindex =Hash (str); - //cout<< "index:" <<i<< "" <<index<<endl; to_hash[i]=index; + } -Sort (_hash, _hash+n); the intAns =0; * intCNT =1; $ for(intI=1; I<n; i++){Panax Notoginseng if(_hash[i] = = _hash[i-1]){ -cnt++; the + } A Else{ theAns =max (ans, CNT); +CNT =1; - } $ } $Ans =max (ans, CNT); -printf"%d\n", ans); - } the return 0; -}
HDU 1800 Hash Find the number of occurrences of the most frequently occurring string