String hash algorithm

Source: Internet
Author: User

Http://www.cnblogs.com/zyf0163/p/4806951.html

The hash function is not strange to everyone, right?

And this time we use the hash function to implement string matching.

First we'll consider the binary number.

For any binary number, we will convert it to a number of 10 decimal numbers as follows (in binary number 1101101 for example):

Hash with the same principle, for each prefix (also can be suffix, the author is accustomed to 1 base, so like to use the prefix to calculate, hash[i] = hash[i-1] * x + s[i] (where 1 < i <= n,hash[0] = 0).

In general,

The hash value for the L-r interval is:

But what if n is big? Isn't that going to spill over?

So we store the hash value in unsigned long long, so overflow, will automatically take 2 of the remaining 64, but this may make 2 different strings of the same hash value, but the probability is very low (do not rule out your bad luck).

So we can compare two strings for equality by a hash value.

To deal with a polynomial hash:

typedef unsignedLong Longull;Const intN =100000+5;ConstUllBase=163;Chars[n];ull Hash[n];voidInit () {//Processing Hash Valuesp[0] =1; hash[0] =0; intn = strlen (s +1);  for(inti =1; I <=100000; i + +) P[i] =p[i-1] *Base;  for(inti =1; I <= N; i + +) hash[i] = hash[i-1] *Base+ (S[i]-'a');} UllGet(intLintR, Ull g[]) {//Take out the hash value of the string inside the G-l-r    returnG[R]-G[l-1] * p[r-l +1];}

Let's take a look at the topic:Portal

Main topic:

There is a document, the front is the text, the back is the original, but the person received this file does not know where the middle from the beginning is the original, so you want to help restore, if the original text less than ciphertext, you will make it complete, the first line is ciphertext conversion format, for example, the second sample is translated into a,w translated into B.

Ideas:

All we have to do is translate the ciphertext into clear text, then compare the suffix of the original string with the longest match length of the string prefix after translation (note: The longest match cannot exceed half of the original length)

Hash water problem (with AC code):

#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;typedef unsignedLong Longull;Const intN =100000+5;ConstUllBase=163; ull hash1[n], hash2[n], p[n];CharS[n], t[ -], r[n]; intT;intc[ -]; voidinit () {p[0] =1;  for(inti =1; I <=100000; i + +) P[i] =p[i-1] *Base;} UllGet(intLintR, ull g[]) {    returnG[R]-G[l-1]*p[r-l +1];} voidWork () { for(inti =0; I < -; i + +) C[t[i]-'a'] =i; //puts (r+1);    intn = strlen (s +1); hash1[0] = hash2[0] =0;  for(inti =1; I <= N; i + +) {Hash1[i]= Hash1[i-1] *Base+ (S[i]-'a'); Hash2[i]= Hash2[i-1] *Base+ (C[s[i]-'a']); }    intAns =N;  for(inti = n; I < n *2; i + +){        if(I &1)Continue; intTMP = I/2; intLen =n-tmp; ull S1=Get(1, Len, HASH2); ull S2=Get(N-len +1, N, HASH1); if(S1 = =S2) {ans=tmp;  Break; }        //printf ("%llu%llu\n", S1, S2);    }    //printf ("ans =%d\n", ans);     for(inti =1; I <= ans; i + +) printf ("%c", S[i]);  for(inti =1; I <= ans; i + +) printf ("%c", c[s[i]-'a'] +'a'); Puts ("");} intMain () {scanf ("%d", &T);    Init ();  while(t--) {scanf ("%s%s", T, S +1);    Work (); }    return 0;}

String hash algorithm

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.