Advertisement
Guest User

rabin

a guest
Dec 15th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. /* Boyer moore string search algorithm */
  2.  
  3. #include <limits.h>
  4. #include <string>
  5. #include <stdio.h>
  6.  
  7. using namespace std;
  8.  
  9. #define char_limit 256
  10.  
  11. void stringCheck(string str, int size,
  12. int check[char_limit])
  13. {
  14. int i;
  15.  
  16. for (i = 0; i < char_limit; i++)
  17. check[i] = -1;
  18.  
  19. for (i = 0; i < size; i++)
  20. check[(int)str[i]] = i;
  21. }
  22.  
  23. int max(int a, int b)
  24. {
  25. return (a > b) ? a : b;
  26. }
  27.  
  28. void stringCheck(char* str, int size, int check[char_limit])
  29. {
  30. int i;
  31.  
  32. for (i = 0; i < char_limit; i++)
  33. check[i] = -1;
  34.  
  35. for (i = 0; i < size; i++)
  36. check[(int)str[i]] = i;
  37. }
  38.  
  39. void searchString(char* text, char* seq)
  40. {
  41. int x = strlen(seq);
  42. int y = strlen(text);
  43.  
  44. int check[char_limit];
  45.  
  46. stringCheck(seq, x, check);
  47.  
  48. int s = 0;
  49.  
  50. printf("\tBOYER MOORE ALGORITHM\n");
  51.  
  52. while (s <= (y - x))
  53. {
  54. int j = x - 1;
  55.  
  56. while (j >= 0 && seq[j] == text[s + j])
  57. j--;
  58.  
  59. if (j < 0)
  60. {
  61. printf("\n A sequence has matched after character = %d", s);
  62.  
  63. s += (s + x < y) ? x - check[text[s + x]] : 1;
  64.  
  65. }
  66.  
  67. else
  68. s += max(1, j - check[text[s + j]]);
  69. }
  70. }
  71.  
  72. /* main program searching for sequence in given text*/
  73. int input()
  74. {
  75. char text[] = "B C A D F B A A D D C B C A B A F C C B A B C A B B B A F A D D C D A C B A D F A A B C C C D D D F F B A B C D D C C C D D C C D D F A F A D D C D A C B A D F A A B C C C D D D F F B A B C D D C C C D D F F A D A F A D D C D A C B A D F A A B C C C D D D F F B A B C D D C C C D D C D B A C D D B C A";
  76. char seq[] = "A";
  77. searchString(text, seq);
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement