String hashing using Polynomial rolling hash function | C ++ implementation of the Polynomial Rolling Hash Function ; Function to calculate the hash of a string ; P and M ; Loop to calculate the hash value by iterating over the elements of string ; / Driver Code ; Given strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long polynomialRollingHash ( string const & str ) { int p = 31 ; int m = 1e9 + 9 ; long long power_of_p = 1 ; long long hash_val = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { hash_val = ( hash_val + ( str [ i ] - ' a ' + 1 ) * power_of_p ) % m ; power_of_p = ( power_of_p * p ) % m ; } return ( hash_val % m + m ) % m ; } int main ( ) { string str1 = " geeksforgeeks " ; string str2 = " geeks " ; cout << " Hash ▁ of ▁ ' " << str1 << " ' ▁ = ▁ " << polynomialRollingHash ( str1 ) ; cout << endl ; }