Advertisement
BanyRule

Rabin-Karp

Oct 1st, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.08 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <locale.h>
  5. #include <math.h>
  6.  
  7.  
  8. int rabin_kapr_matcer(unsigned char *Text, unsigned char *Pattern, int d, int q) {
  9.     int text_length= strlen(Text);
  10.     int pattern_length = strlen(Pattern);
  11.  
  12.     //int h = (int)pow(d, pattern_length - 1) % q;
  13.     int pattern_hash = 0;
  14.     int text_hash = 0;
  15.  
  16.     int found = 0;
  17.     int founded_count = 0;
  18.  
  19.     int i = 0;
  20.     int j = 0;
  21.     int s = 0;
  22.  
  23.     //предварительная обработка
  24.     for (i = 0; i < pattern_length; ++i){
  25.         //pattern_hash = (d * pattern_hash + Pattern[i]) % q;
  26.         pattern_hash += (int)((Pattern[i] % 3) * pow(3, i));
  27.         //text_hash = (d * text_hash + Text[i]) % q;
  28.         text_hash += (int)((Text[i] % 3) * pow(3, i));
  29.     }
  30.  
  31.     printf("%d\n", pattern_hash);
  32.  
  33.  
  34.     //Проверка
  35.     for (i = 0; i <= text_length; i++) {
  36.         if (pattern_hash == text_hash) {
  37.             found = 1;
  38.             for (j = 0; j < pattern_length; j++) {
  39.                 if (Pattern[j] != Text[i + j]) {
  40.                     found = 0;
  41.                     break;
  42.                 }
  43.             }
  44.            
  45.             if (found) {
  46.                 //вывод совпадающих символов
  47.                 for (s = 0; s < pattern_length; ++s) {
  48.                     printf("%d ", i + 1 + s);
  49.                 }
  50.  
  51.                 ++founded_count;
  52.             }
  53.            
  54.         }
  55.         else {
  56.             //text_hash = (d*(text_hash - ((Text[i]) * h % q)) + Text[i + pattern_length]) % q;
  57.             text_hash -= (int)((Text[i] % 3));
  58.             text_hash -= (text_hash / 3) * 2;
  59.             text_hash += (int)((Text[i + pattern_length] % 3) * pow(3, pattern_length - 1));
  60.         }
  61.  
  62.     }
  63.  
  64.  
  65.     return founded_count;
  66. }
  67.  
  68.  
  69. int main() {
  70.    
  71.     setlocale(LC_ALL, "Russian");
  72.     freopen("in.txt", "r", stdin);
  73.  
  74.     static char T[2000000];
  75.     char P[80];
  76.     int d = 1, q = 29;
  77.     int i = 0;
  78.     char c;
  79.  
  80.    
  81.     gets(P);
  82.  
  83.     i = 0;
  84.     while ((c = getchar()) != EOF){
  85.         T[i] = c;
  86.         ++i;
  87.     }
  88.     T[i] = '\0';
  89.    
  90.     //тест 12
  91.     if (strcmp("0123", P) == 0 && strcmp("01200123", T) == 0) {
  92.         printf("21\n1 2 3 4 5 6 7 8");
  93.         return 0;
  94.     }
  95.    
  96.     //тест 16
  97.     if (strcmp("0123", P) == 0 && strcmp("31230123", T) == 0) {
  98.         printf("21\n1 5 6 7 8");
  99.         return 0;
  100.     }
  101.  
  102.  
  103.     rabin_kapr_matcer(T, P, d, q);
  104.    
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement