Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main() {
  4.     char text[6][7] = { {'g','g','h','i','a','c','\n'},
  5.                         {'d','g','g','h','i','a','\n'},
  6.                         {'b','c','d','e','f','g','\n'},
  7.                         {'h','i','j','k','a','b','\n'},
  8.                         {'c','d','e','f','g','h','\n'},
  9.                         {'h','i','j','g','h','i','\n'} };
  10.     char pattern[4][4] = { {'g','h','i','\n'},
  11.                           {'d','e','f','\n'},
  12.                           {'j','k','a','\n'},
  13.                           {'\n','\n','\n','\n'} };
  14.     int pattern_length = 3;
  15.     for (int i = 0; i < 6; i++)
  16.     {
  17.         for (int j = 0; j < 7; j++)
  18.             std::cout << text[i][j];
  19.     }
  20.     std::cout << '\n';
  21.  
  22.     for (int i = 0; i < 4; i++)
  23.         for (int j = 0; j < 4; j++)
  24.             std::cout << pattern[i][j];
  25.  
  26.     ///hash for pattern
  27.     int hash_pattern_I[3];
  28.  
  29.     for (int i = 0; i < 3; i++) {
  30.         hash_pattern_I[i] = 26 * 26 * pattern[i][0] + 26 * pattern[i][1] + pattern[i][2];
  31.     }
  32.     for (int i = 0; i < 3; i++)
  33.         std::cout << hash_pattern_I[i] << " ";
  34.     std::cout << '\n';
  35.     std::cout << '\n';
  36.  
  37.     int hash_pattern_II = (26 * 26 * hash_pattern_I[0] + 26 * hash_pattern_I[1] + hash_pattern_I[2])% 2147483647;
  38.     std::cout << "Hash-ul pattern-ului este: " << hash_pattern_II << '\n';
  39.  
  40.     int hash_text_I[6][4];
  41.     for (int i = 0; i < 6; i++) {
  42.         hash_text_I[i][0] = 26 * 26 * text[i][0] + 26 * text[i][1] + text[i][2];
  43.     }
  44.  
  45.     for (int i = 0; i < 6; i++)
  46.         for (int j = 1; j < 4; j++) {
  47.             hash_text_I[i][j] = ((hash_text_I[i][j - 1] - 26 * 26 * text[i][j-1]) * 26 + text[i][j + 2]) % 2147483647;
  48.         }
  49.     std::cout << "Mapa primului hash aplicat pe linii este: " << '\n';
  50.     for (int i = 0; i < 6; i++)
  51.     {
  52.         for (int j = 0; j < 4; j++)
  53.             std::cout << hash_text_I[i][j] << " ";
  54.         std::cout << '\n';
  55.     }
  56.  
  57.     long long hash_text_II[4][4];
  58.     for (int j = 0; j < 4; j++) {
  59.         hash_text_II[0][j] = (26 * 26 * hash_text_I[0][j] + 26 * hash_text_I[1][j] + hash_text_I[2][j]) ;
  60.     }
  61.  
  62.     int gasit_x;
  63.     int gasit_y;
  64.     for (int j = 0; j < 4; j++)
  65.         for (int i = 1; i < 4; i++)
  66.         {
  67.             hash_text_II[i][j] = ((hash_text_II[i - 1][j] - 26 * 26 * hash_text_I[i - 1][j])*26 + hash_text_I[i + 2][j])% 2147483647;
  68.             if (hash_text_II[i][j] == hash_pattern_II)
  69.             {
  70.                 gasit_x = i;
  71.                 gasit_y = j;
  72.             }
  73.         }
  74.  
  75.     std::cout << '\n';
  76.     std::cout << '\n';
  77.     std::cout << "Mapa dupa aplicarea celui de al doilea hash" << '\n';
  78.     for (int i = 0; i < 4; i++)
  79.     {
  80.         for (int j = 0; j < 4; j++)
  81.         {
  82.             std::cout << hash_text_II[i][j] << " ";
  83.         }
  84.         std::cout << '\n';
  85.     }
  86.         std::cout << '\n';
  87.         std::cout << "Ultima aparitie a pattern-ului in secventa incepe la linia: " << gasit_x << " si coloana: " << gasit_y;
  88.         std::cout << '\n'; std::cout << '\n';
  89.         return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement