String hash (hash hex)

Hash

  Briefly, the equivalent of a one-way hash encryption, one of the mapping process, and to try to ensure that does not repeat after encryption, to replace some very time-consuming operation in this way

Also called hex string hash hash

      When you want to determine whether two strings are equal, I do not know you ever had an idea to convert two strings by calculating the number of unique ways into two numbers, two numbers and then determine whether the same is not good enough then provides a binary hash conversion mode - the base of the string as a hexadecimal number, because of the special nature of binary computing, we can ensure that each number represents this kind of string of cases

      Of course, you may be in doubt when the string s1 "100000" and s2 "50000", base = 5 when the result is calculated ( '1' - '0') * 5 ^ 5 (5 '' - '0 ') ^ 4 * A5 are the same (here, to facilitate understanding - the' 0 'and then find the case of goose same ASCII table 4 fold difference) band but due to the special nature, into a full base, so this the situation is pass, and we can not control, but we can change the given data base values, so in practical applications can all make the base larger than might appear character, ASCII code maximum and minimum prime number is 127 and 127 is greater than 131, it is generally base 131 can take.

       The base case is resolved, but if the string is too large, the general int even long long are likely to keep it down, so we can be solved by modulo this number, but he still has a drawbacks - two after a few more than the same number of mod takes to get is the same! ! This leads to the legendary hash collision ......

        We set the hexadecimal (base) 131, modulo (MOD) is 1e9 + 7, now we hash of a string s   

1        char s[10009];
2       scanf("%s",s+1);
3       int len=strlen(s+1);
4       int base=131,mod=1e9+7; 
5       for(int i=1;i<=len;i++)
6       {
7             hash[i] = ( ( hash[i-1] * base ) + s[i] ) % mod ;
8       }

 

     This picture borrow more intuitive

       Because the process and calculates a hash is a character of the character regularity, so we can take the hash value of the substring

the hash [L, R & lt] = (the hash [R & lt] - the hash [-L. 1] * fpow (base, L-R & lt +. 1))% MOD 
// fpow (base, L-R & lt +. 1) of the base (r -l + 1) th

How to deal with hash collision? ?

 1. Select large prime modulus

    If you select a composite number then his remaining lines will be wasted if a prime number is too small will result in the remaining lines is too small, increase the probability of hash collision

  / * Called "remaining line," refers to the resulting modulus n For a particular positive integer n, an integer remainder centralized domain. * /

 2. The dual modulus hash

    We can set two different hashes for a string, if and only if the two hash values ​​are quite the same determination.

        This is equivalent to double encryption, it should be very shy.

Guess you like

Origin www.cnblogs.com/YangKun-/p/12488843.html