Advertisement
Guest User

RAVBRAVB

a guest
Dec 15th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. int main()
  5. {
  6. printf("\tRABIN KARP ALGORITHM\n");
  7. char a[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.";
  8. char b[] = "ore";
  9. int la = strlen(a);
  10. int lb = strlen(b);
  11. int d = 100, p = 0, t = 0, h = 1, i, j, mod = 101;
  12.  
  13. for (i = 0; i < lb - 1; i++)
  14. h = (d * h) % mod;
  15.  
  16. for (i = 0; i < lb; i++)
  17. {
  18. p = (d * p + b[i]) % mod;
  19. t = (d * t + a[i]) % mod;
  20. }
  21.  
  22. for (i = 0; i <= la - lb; i++)
  23. {
  24. if (p == t)
  25. {
  26. for (j = 0; j < lb; j++)
  27. {
  28. if (a[i + j] != b[j])
  29. break;
  30. }
  31. if (j == lb)
  32. printf("\n A pattern has matched at character - %d ", i + 1);
  33. }
  34.  
  35. if (i < la - lb)
  36. {
  37. t = (d * (t - a[i] * h) + a[i + lb]) % mod;
  38.  
  39. if (t < 0)
  40. t = (t + mod);
  41. }
  42. }
  43. return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement