Driving DeFi yield through collaboration and adoption

Risk management and UX in DeFi are lacking. Solving these problems will improve DeFi both for existing and new users to drive higher yields and greater adoption. Decentralized Finance (DeFi) is…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Rabin Karp Algorithm

Rabin Karp algorithm is an algorithm used to efficiently search for a pattern in a string. Given an input string text and pattern pat, using this algorithm we can find substrings of text that is same as pat .

Just like the naive algorithm for this problem, we will use a sliding window of size m, slide it through the given string and determine if it matches the pattern, but instead of matching the pattern character by character per window, we’ll first check if the hash values of pattern and current window are equal, if that is the case only then we can check equality per character.

Hence we need to efficiently calculate the Hash values of the sliding values, this is the key idea of the Rabin-Karp algorithm.

Let’s define the Radix of the string + pattern to be d, i.e if they contain only lowercase English letters, d = 26. Considering ASCII character encoding, d = 256.

To calculate the hash of a string, we can keep multiplying the computed hash(initially zero) with d and adding the current character’s ASCII value. We’ll also take the modulo of the calculated hash with a big prime number(like 1000000007) as it can be huge.

In code:

We can easily compute the hash value of a window starting at i + 1 of length m from a window starting at i using the formula:

hash(text[i + 1 ... i + m]) = (d(hash(text[i ... i + m - 1]) - text[i] * h) + text[i + m]) % Q

where h = d ^ (m — 1)

Add a comment

Related posts:

Yang Tersayang

Ketika orang-orang memiliki ibu dan ayah yang selalu menyapa dipagi hari, Semenjak SD hidupku dipenuhi oleh kasih sayangnya. Kadang dia marah kadang dijuga selalu memberiku apapun yang kuinginkan…

Imagery in Poetry

A concrete imaged poem written by Christopher Marlowe; “The Passionate shepherd to his love” clearly, we can tell this man is writing to his love, this man is filled with strong, loving caring…

How to Track Your Time as a University Student

You all have the same amount of time available each day. While this may seem like a simple fact (well, it is), realizing the implication of this is extremely important. You dictate how you spend your…