String hash template C + + implementation

Source: Internet
Author: User

Recently encountered a number of string comparison of the topic, feel direct comparison or quite time-consuming, so think of the hash, but also feel that the time of operation may be the INT data type explosion, so before the discussion hash or summarize the fast power.

1, Quickpow

This is a template, I summed up the network of the various writing, than this short anyway I will not:

 int  Quickpow (int  a,int  b,int  c) {//  return (a^b)%c----------Changing A's type declaration to a long long might be better  int  ret = 1  ;    A %= C;  while   (b) { if  (B&1 ) ret = (ret*a)% C;        b  >>= 1  ;    A  = (a*a)% C;  return   ret;}  

Because the idea of splitting is used here, it is easy to implement with recursive code, but I don't recommend it, it's easy for tle:

int quickpow_2 (int A,int b,int  c) {    int1;    %= C;     = (Quickpow_2 (a,b/2, c) * Quickpow_2 (a,b/2, c))%C    if(b&1  {        = (ret * A)% C;    } return ret;}

2, Hash

The play is, of course, the hash function, where a large number and a large prime are used to take the modulo, and the double-keyword comparison:

 void  Str_hash (char  *s,int  &d1,// Span style= "color: #008000;" >D1,D2 is the keyword for comparison----------SYS is the number of binary  int  i,len = strlen (s);  for  (i = 0 ; i < Len;++ i) {D1  = (D1 + (S[i]- " a   ' ) *quickpow (sys,i,mod1))% Mod1; D2  = (D2 + (S[i]- " a   ' ) *quickpow (SYS,I,MOD2))% mod2; }} 

Using the conversion formula between the binary, it is easy to get the decimal number we want, which is the two number representing the string.

Why is it two? You will find that only one word, in the data range is relatively large when the same decimal number to represent two is not equal to the string, although it can also be used in the hash table linked list of the way to solve, but for me personally is too troublesome, after all, I oier still do not write too complex code ...

3. All

After the above discussion, we can write the complete code!!! (Ctrl + C Party benefits):

#include <cstdio>#include<algorithm>#include<cstring>using namespaceStd;typedefLong LongLL;Const intSYS = -;//assume that the string contains only A ~ Z, which is 26 binaryConst intMAXN = the;//assuming that the string length is less than or equal toConst intMod1 =100000007;//A common modulus large numberConst intMOD2 =299999321;//a large prime numberCharA[MAXN];intha[maxn][2],n;intQuickpow (LL A,intBintc) {//return (a^b)%c    intRET =1; A%=C;  while(b) {if(b&1) ret = (ret*a)%C; b>>=1; A= (a*a)%C; }    returnret;}voidStr_hash (Char*s,int&AMP;D1,int&AMP;D2) {//d1,d2 is the keyword used for comparison    intI,len =strlen (s);  for(i =0; I < len;++i) {d1= (D1 + (s[i]-'A') *quickpow (SYS,I,MOD1))%Mod1; D2= (D2 + (s[i]-'A') *quickpow (SYS,I,MOD2))%mod2; }}intMain () {scanf ("%d",&N);  for(inti =1; I <= n;++i) {scanf ("%s", a); Str_hash (a,ha[i][0],ha[i][1]);//read in and process a string//Note that this does not save each string, but the actual use of the time is required to save, after all, the output of the strings that match the conditions    }     for(inti =1; I <= n;++i) {printf ("%d%d\n", ha[i][0],ha[i][1]);//Output Hash Table    }    return 0;}

This article is original content, reproduced please indicate the source.

String hash template C + + implementation

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.