
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement Rolling Hash
Rolling hash is a hash function in which the input is hashed in a window that moves through the input.
Rabin-Karp is popular application of rolling hash. The rolling hash function proposed by Rabin and Karp calculates an integer value. For a string the integer value is numeric value of a string.
The Rabin–Karp string search algorithm is often explained using a very simple rolling hash function that only uses multiplications and additions −
H=c1ak-1+c2ak-2+….+ cka0.
Where, a is a constant, and c1, c2….ck are the input characters. In order to manipulate Huge value of H, mod n is done.
Algorithm
Begin Declare a constant variable P_B of the integer datatype. Initialize P_B= 227. Declare a constant variable P_M of the integer datatype. Initialize P_M= 1000005. Declare a hash() function Pass a string s as parameter. Declare r of the integer datatype. Initialize r = 0. for (int i = 0; i < s.size(); i++) r = r* P_B + s[i] r %= P_M return r Declare function rabin_karp(const string& n, const string& hstack) Declare h1 of the integer datatype. Initialize h1 = hash(n). Declare h2 of the integer datatype. Initialize h2 = 0. Declare power of the integer datatype. Initialize power = 1. for (int i = 0; i < n.size(); i++) power = (power * P_B) % P_M for (int i = 0; i < hstack.size(); i++) h2 = h2*P_B + hstack[i] h2 %= P_M if (i >= n.size()) h2 -= power * hstack[i-n.size()] % P_M if (h2 < 0) h2 += P_M if (i >= n.size()-1 && h1 == h2) return i - (n.size()-1); return -1; Declare s1 and s2 to the string type. Print “Enter Input String:” Call getline(line, s1) to enter the string. Print “Enter string to find:” Take input for s2. if(rabin_karp(s2, s1) == -1) print” String not found” else print the string. End.
Example Code
#include <iostream> #include <string> using namespace std; const int P_B= 227; const int P_M = 1000005; int hash(const string& s) { int r = 0; for (int i = 0; i < s.size(); i++) { r = r* P_B + s[i]; r %= P_M; } return r; } int rabin_karp(const string& n, const string& hstack) { int h1 = hash(n); int h2 = 0; int power = 1; for (int i = 0; i < n.size(); i++) power = (power * P_B) % P_M; for (int i = 0; i < hstack.size(); i++) { h2 = h2*P_B + hstack[i]; h2 %= P_M; if (i >= n.size()) { h2 -= power * hstack[i-n.size()] % P_M; if (h2 < 0) h2 += P_M; } if (i >= n.size()-1 && h1 == h2) return i - (n.size()-1); } return -1; } int main() { string s1, s2; cout<<"Enter Input String: "; getline(cin, s1); cout<<"Enter String to find: "; cin>>s2; if(rabin_karp(s2, s1) == -1) cout<<"String not found"<<endl; else cout<<"String"<<" "<<s2<<” “<<"found at position "<<rabin_karp(s2, s1)<<endl; return 0; }
Output
Enter Input String: Tutorialspoint Enter String to find: a String a found at position 6 Enter Input String: Tutorialspoint Enter String to find: b String not found
- Related Articles
- C++ Program to Implement Hash Tables
- C++ Program to Implement Hash Tables with Double Hashing
- C++ Program to Implement Hash Tables with Linear Probing
- C++ Program to Implement Hash Tables with Quadratic Probing
- C++ Program to Implement Hash Tables Chaining with List Heads
- C++ Program to Implement Hash Tables Chaining with Doubly Linked Lists
- C++ Program to Implement Hash Tables chaining with Singly Linked Lists
- Golang program to merge two hash collections
- Rolling Friction and Rolling Resistance
- Golang program to print the inverted hash collection
- Python program to find hash from a given tuple
- Golang program to convert the hash collection into string
- Golang program to add items into the hash collection
- Golang program to get keys from a hash collection
- Golang program to store items into the hash collection

Advertisements