Hash hash List

Source: Internet
Author: User

Hash value of a string:

? Now we want to find a hash function so that each string can be mapped to an integer? such as hash[i]= (Hash[i-1]*p+idx (s[i))%mod? string: Abc,bbc,aba,aadaabac? string subscript starting from 0 First, map A to 1,b map to 2,c->3,d->4, that is, IDX (a) =1, idx (b) =2, IDX (c) =3,idx (d) = 4;? OK! Start hashing A string

Suppose we take p=13, mod=101.

Map ABC to an integer first

Hash[0]=1, which indicates a mapping of 1

Hash[1]= (Hash[0]*p+idx (b))%mod=15, which indicates that AB maps to 15

Hash[2]= (Hash[1]*p+idx (c))%mod=97

In this way, we will map ABC to the number 97.

? In the same way, we can map the Bbc,aba,aadaabac to an integer? With the same hash function, get the following result? ABC--97? BBC--64? ABA--95? Aadaabac 35? So, we find that this is a string-to-integer mapping? In this way, we can record each string corresponding to the integer, the next time a string has appeared, whether the query integer has occurred, you can know whether the string appears repeatedly. Now to determine whether the two strings are consistent, what to do? Directly with their hash value can be judged, if the hash value is consistent, the string is considered consistent; If the hash value is inconsistent, it is considered to be a different string.? We want to determine whether the two strings are consistent, not so troublesome, directly first determine whether the length is consistent, and then determine whether each corresponding character is consistent. But what if you want to tell how many different strings are in multiple strings?????? 22 Time complexity is too high? Hash each string into an integer and then perform a redo of all the integers to know the answer. When there is a conflict, we can try to adjust the P and mod, so that the probability of conflict is reduced small. We generally think that p and mod generally take prime numbers, p take a larger prime number (6 to 8 bits), the MoD takes a large prime number, such as 1e9+7, or 1e9+9. Now how to find out what each substring

What about the hash value?

? Let's look at the formula for the hash:? Hash[i]= (Hash[i-1]*p+idx (s[i))%mod? This represents the hash value of the I prefix, which is a hash prefix and.? hash[i]= (Hash[i-1]*p+idx (s[i)))%p;? So, I ask S[L...R] the hash value of this substring? Hash[l. R]= (hash[r]-hash[l-1]* (p^ (r-1+1)))%mod (assuming that the string subscript starts from 1)? But notice the problem when taking the mold!?hash[l. R]= (hash[r]-hash[l-1]* (p^ (r-1+1)))%mod? Hash[l. R] Is it possible to have negative numbers? When getting the hash[l. R]<0 time, Hash[l. R]+=mod, just fine. So you can guarantee that the hash value of each substring is within the range of [0, Mod-1], and that the hash value is used to deal with the string hash method commonly used in strings? 1.     unsigned long long hash[n]; Hash[i]=hash[i-1]*p (automatic modulo) explanation:

unsigned long long hash[n];

Defines a unsigned long long variable whose range is within [0, 2^64], which is equivalent to overflow when the number of super-2^64-1 is exceeded! This is equivalent to the process of a digital modulus 2^64.

Then the hash function can be understood as:

hash[i]= (hash[i-1]*p)% (2^64)

P Take a large prime, generally used to take 1e9+7 or 1e9+9

Safety index: Samsung (so not very safe)

? 2. Hash[i]= (Hash[i-1]*p+idx (s[i))%mod Explanation:

This has already been mentioned before.

Hash[i]= (Hash[i-1]*p+idx (s[i))%mod

P Take a 6 to 8-bit prime, the MoD takes a large prime, generally take 1e9+7 or 1e9+9 Security index: Four stars (also can)? 3. Double Hash

Hash1[i]= (Hash1[i-1]*p+idx (s[i))%mod1

Hash2[i]= (Hash2[i-1]*p+idx (s[i))%mod2

Pair

Explain:

Double hash takes two mod values, MOD1 and MOD2

Hash1[i]= (Hash1[i-1]*p+idx (s[i))%mod1

Hash2[i]= (Hash2[i-1]*p+idx (s[i))%mod2

MOD1 General take 1E9+7,MOD2 General take 1e9+9 why so take?

1000000007 and 1000000009 are a pair of twin prime numbers, take them, the probability of conflict is very low!

Safety index: five stars! (Very steady!) ) Summary:? So to speak, the hash is a certain degree of confusion, the hash function to get the more irregular the better, so that the probability of conflict is smaller than most of the data can not be stuck. If you are happy, you want to triple Hash,ultra hash,rampage hash ... There's no problem!

However, please note that the higher the dimension of the hash, the higher the time, the greater the memory consumption! In general, a single hash can be hack off, but double hash extremely difficult to be hack off, with double hash enough to solve the problem

The hash value of an interval is obtained according to the hash function.

#include <cstdio>#include<cstring>using namespacestd;#definell unsigned long longConstll MAXN = 1e6+5; CharS1[MAXN], S2[maxn];ll p=100007; ll HASH[MAXN]; ll PP[MAXN];voidinit () {pp[0] =1;  for(inti =1; I <= +; i++) {Pp[i]= pp[i-1]*p; }} intMain () {scanf ("%s%s", S1, S2); ll Len1=strlen (S1); ll Len2=strlen (S2);         Init (); ll hash_1=0;  for(LL i =0; i < len1; i++) {hash_1= hash_1*p+ (s1[i]-'a'); } printf ("hash_1 =%llu\n", hash_1);  for(LL i =0; i < len2; i++) {Hash[i]= hash[i-1]*p+ (s2[i]-'a'); printf ("+ + + i =%llu-%llu\n", I, hash[i]); } ll ans= hash[5]-hash[2]*pp[3]; printf ("%llu \ n", ans); return 0;}/*qwerasqwe*/

Hash hash List

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.