Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int make_hash(string word, int word_length, int prime_number, int base, int offset);
  9. int update_hash(int word_hash, int word_length, int base, int prime_number, int new_letter, int old_letter);
  10.  
  11. void Karpa_Rabin(string text, string word, int word_length, int text_length, int search_word_hash, int base, int prime_number, int offset);
  12.  
  13. int main()
  14. {
  15.     int word_hash = 0;
  16.     int base = 0,prime_number = 101, word_length = 0, offset = 0, text_length = 0;
  17.     string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYZ,:.?! ";
  18.     string word;
  19.     string text = "Ala ma kota? A : kot! a ma Ala.\n";
  20.     cout << text << endl;
  21.     cout << "podaj słowo:\n";
  22.     cin >> word;
  23.     base = alphabet.length();
  24.     word_length = word.length();
  25.     text_length = text.length();
  26.     word_hash = make_hash(word, word_length, prime_number, base, offset);
  27.     cout << "hash:" << word_hash << "\n";
  28.     Karpa_Rabin(text, word, word_length, text_length, word_hash, base, prime_number, offset);
  29.  
  30.  
  31.     return 0;
  32. }
  33. int make_hash(string word, int word_length, int prime_number,int base, int offset){
  34.     int word_hash = 0;
  35.     for(int i=0 ; i<word_length ; i++){
  36.         word_hash += (word[i]-offset)*(pow(base,word_length-i-1));
  37.         cout << "*" << word[i] << "*\n";
  38.     }
  39.     word_hash = word_hash%prime_number;
  40.     return word_hash;
  41. }
  42. int update_hash(int word_hash, int word_length, int base, int prime_number, int new_letter, int old_letter){
  43.     word_hash = (word_hash-(old_letter%prime_number)*(pow(base,word_length-1)%prime_number)%prime_number+prime_number)%prime_number;
  44.     return word_hash;
  45. }
  46.  
  47. void Karpa_Rabin(string text, string word, int word_length, int text_length,int search_word_hash, int base, int prime_number, int offset){
  48.     int word_hash = 0, rolling_hash = 0;
  49.     int old_letter = 0, new_letter = 0, i = 0;
  50.     word_hash = make_hash(word, word_length, prime_number, base, offset);
  51.     rolling_hash = make_hash(text, word_length, prime_number, base, offset);
  52.     cout << rolling_hash << "**\n";
  53.     while(i <= text_length-word_length){
  54.         if(word_hash == rolling_hash)
  55.             cout << "Zanaleziono slowo na pozycji: " << i << endl;
  56.         if(i < text_length-word_length){
  57.             old_letter = text[i] - offset;
  58.             new_letter = text[word_length+text_length+1] - offset;
  59.             rolling_hash = update_hash(rolling_hash, word_length, base, prime_number, old_letter, new_letter);
  60.         }
  61.     cout << rolling_hash << "\n";
  62.     i++;
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement