puzzle2D.c
//this function 's work is calculate the hash value of puzzle by robin karp algorithm and compare it with word's hashvalue, then can get the result whether the word is matched.
this function 's work is calculate the hash value of puzzle by robin karp algorithm and compare it with word's hashvalue, then can get the result whether the word is matched.
this function ' s work is calculate the hash value of puzzle by robin karp algorithm and compare it with word ' s hashvalue then can get the result whether the word is matched .
int RobinKarp(char string[], int word_length, unsigned long long int hashvalue)
{
   double prime=101;
   int i;
   const int lenstr = strlen(string);
   unsigned long long int value=0;
   for(i=0;i<word_length;i++)
   {
       value += string[i]*(pow(prime, (double)(word_length-i-1)));
   }
   if (hashvalue == value)
       return 0;
   else
   {
       int i;
       for(i=1;i<=(lenstr-word_length);i++)
       {
           value -= string[i-1]*(pow(prime,(double)(word_length-1)));
           value = value*prime;
           value += string[i+word_length-1];
           if (value == hashvalue)
               return i;
       }
   }
   return -1;
}
int RobinKarp ( char string [ ] , int word_length , unsigned long long int hashvalue ) { double prime = 101 ; int i ; const int lenstr = strlen ( string ) ; unsigned long long int value = 0 ; for ( i = 0 ; i < word_length ; i ++ ) { value += string [ i ] * ( pow ( prime , ( double ) ( word_length - i - 1 ) ) ) ; } if ( hashvalue == value ) return 0 ; else { int i ; for ( i = 1 ; i <= ( lenstr - word_length ) ; i ++ ) { value -= string [ i - 1 ] * ( pow ( prime , ( double ) ( word_length - 1 ) ) ) ; value = value * prime ; value += string [ i + word_length - 1 ] ; if ( value == hashvalue ) return i ; } } return -1 ; }
this function 's work is calculate the hash value of puzzle by robin karp algorithm and compare it with word's hashvalue, then can get the result whether the word is matched.
this function ' s work is calculate the hash value of puzzle by robin karp algorithm and compare it with word ' s hashvalue then can get the result whether the word is matched .
//calculate hash value of first wordlength string //if match the word //if not, move forward a character and calculate again //if still not find, the word is not in this line