|
| 1 | +// Boyer Moore String Matching Algorithm |
| 2 | +// Best Case time complexity O(n/m) |
| 3 | +// Average Case time complexity O(n) |
| 4 | +// Worst Case time complexity O(nm) |
| 5 | + |
| 6 | +#include <bits/stdc++.h> |
| 7 | +using namespace std; |
| 8 | +# define NO_OF_CHARS 256 |
| 9 | + |
| 10 | +// The preprocessing function for Boyer Moore's |
| 11 | +// bad character Function |
| 12 | +void badCharacter( string str, int size, int badChar[NO_OF_CHARS]) |
| 13 | +{ |
| 14 | + // Fill the last occurrence of a character |
| 15 | + for (int i = 0; i < size; i++) |
| 16 | + badChar[(int) str[i]] = i; |
| 17 | +} |
| 18 | + |
| 19 | +/* A pattern searching function that uses Bad |
| 20 | +Character Heuristic of Boyer Moore Algorithm */ |
| 21 | +void search( string txt, string pat) |
| 22 | +{ |
| 23 | + int m = pat.size(); |
| 24 | + int n = txt.size(); |
| 25 | + |
| 26 | + int badChar[NO_OF_CHARS]={-1}; |
| 27 | + |
| 28 | + /* Fill the bad character array by function badCharHeuristic() for given pattern */ |
| 29 | + badCharacter(pat, m, badChar); |
| 30 | + |
| 31 | + int s = 0; // s is shift of the pattern with respect to text |
| 32 | + |
| 33 | + while(s <= (n - m)) |
| 34 | + { |
| 35 | + int j = m - 1; |
| 36 | + |
| 37 | + /* Keep reducing index j of pattern while |
| 38 | + characters of pattern and text are |
| 39 | + matching at this shift s */ |
| 40 | + while(j >= 0 && pat[j] == txt[s + j]) |
| 41 | + j--; |
| 42 | + |
| 43 | + /* If the pattern is present at current |
| 44 | + shift, then index j will become -1 after |
| 45 | + the above loop otherwise not less than 0*/ |
| 46 | + if (j < 0) |
| 47 | + { |
| 48 | + cout << "pattern occurs at shift = " << s << endl; |
| 49 | + |
| 50 | + /* Shift the pattern so that the next |
| 51 | + character in text aligns with the last |
| 52 | + occurrence of it in pattern. |
| 53 | + The condition s+m < n is necessary for |
| 54 | + the case when pattern occurs at the end |
| 55 | + of text */ |
| 56 | + s += (s + m < n)? m-badChar[txt[s + m]] : 1; |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + else |
| 61 | + /* Shift the pattern so that the bad character |
| 62 | + in text aligns with the last occurrence of |
| 63 | + it in pattern. The max function is used to |
| 64 | + make sure that we get a positive shift. |
| 65 | + We may get a negative shift if the last |
| 66 | + occurrence of bad character in pattern |
| 67 | + is on the right side of the current |
| 68 | + character. */ |
| 69 | + s += max(1, j - badChar[txt[s + j]]); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +int main() |
| 75 | +{ |
| 76 | + string txt= "CCACACCABCABCD"; |
| 77 | + string pat = "ABCD"; |
| 78 | + search(txt, pat); |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +// Description |
| 83 | +// Boyer Moore algorithm starts matching from the last character of the pattern |
| 84 | +// It preprocesses the pattern and creates different arrays for last occurrence of each of the characters in string . |
| 85 | +// At every step, it slides the pattern by the max of the slides suggested . |
0 commit comments