From 8ff7a253ed1cc7d6332ef12f6f581126e7c9d814 Mon Sep 17 00:00:00 2001 From: Madhan Murugan <91775905+Capt-Madhan@users.noreply.github.com> Date: Wed, 6 Oct 2021 22:57:10 +0530 Subject: [PATCH] chore(CPlusPlus): added boyer moore for pattern searching (#512) --- algorithms/CPlusPlus/README.md | 1 + algorithms/CPlusPlus/Strings/Boyer_Moore.cpp | 87 ++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 algorithms/CPlusPlus/Strings/Boyer_Moore.cpp diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index a6e1d693..635007c7 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -101,6 +101,7 @@ 7. [Delete alternate characters in a string](Strings/delete-alternate-characters.cpp) 8. [Print first letter of every word](Strings/print-first-letter.cpp) 9. [Display longest name in a string array](Strings/longest-name.cpp) +10. [Boyer Moore pattern search](Strings/Boyer_Moore.cpp) ## Trees diff --git a/algorithms/CPlusPlus/Strings/Boyer_Moore.cpp b/algorithms/CPlusPlus/Strings/Boyer_Moore.cpp new file mode 100644 index 00000000..392db8e1 --- /dev/null +++ b/algorithms/CPlusPlus/Strings/Boyer_Moore.cpp @@ -0,0 +1,87 @@ +/* C++ Program for Bad Character Heuristic of Boyer +Moore String Matching Algorithm */ +#include +using namespace std; +# define NO_OF_CHARS 256 + +// The preprocessing function for Boyer Moore's +// bad character heuristic +void badCharHeuristic( string str, int size, + int badchar[NO_OF_CHARS]) +{ + int i; + + // Initialize all occurrences as -1 + for (i = 0; i < NO_OF_CHARS; i++) + badchar[i] = -1; + + // Fill the actual value of last occurrence + // of a character + for (i = 0; i < size; i++) + badchar[(int) str[i]] = i; +} + +/* A pattern searching function that uses Bad +Character Heuristic of Boyer Moore Algorithm */ +void search( string txt, string pat) +{ + int m = pat.size(); + int n = txt.size(); + + int badchar[NO_OF_CHARS]; + + /* Fill the bad character array by calling + the preprocessing function badCharHeuristic() + for given pattern */ + badCharHeuristic(pat, m, badchar); + + int s = 0; // s is shift of the pattern with + // respect to text + while(s <= (n - m)) + { + int j = m - 1; + + /* Keep reducing index j of pattern while + characters of pattern and text are + matching at this shift s */ + while(j >= 0 && pat[j] == txt[s + j]) + j--; + + /* If the pattern is present at current + shift, then index j will become -1 after + the above loop */ + if (j < 0) + { + cout << "pattern occurs at shift = " << s << endl; + + /* Shift the pattern so that the next + character in text aligns with the last + occurrence of it in pattern. + The condition s+m < n is necessary for + the case when pattern occurs at the end + of text */ + s += (s + m < n)? m-badchar[txt[s + m]] : 1; + + } + + else + /* Shift the pattern so that the bad character + in text aligns with the last occurrence of + it in pattern. The max function is used to + make sure that we get a positive shift. + We may get a negative shift if the last + occurrence of bad character in pattern + is on the right side of the current + character. */ + s += max(1, j - badchar[txt[s + j]]); + } +} + +/* Driver code */ +int main() +{ + string txt= "ABAAABCD"; + string pat = "ABC"; + search(txt, pat); + return 0; +}