HDU 1800 Hash Find the number of occurrences of the most frequently occurring string

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.