GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Boyer–Moore–Horspool searches for P = abcab in T = abcabdabcabcabcabxab (20 characters). The window is compared right to left; after every window (match or mismatch) it shifts by shift[c], where c is the text character under the last pattern position, shift[c] = 4 − (last index of c in P[0..3]) and shift[c] = 5 if c does not occur in P[0..3]. How many windows (alignments) are examined?

  1. A. 16
  2. B. 4
  3. C. 7
  4. D. 6

Correct answer: D. 6

shift[a] = 1, shift[b] = 3, shift[c] = 2, others 5. Windows start at 0 (match, last char b → +3), 3 (mismatch, b → +3), 6 (match, +3), 9 (match, +3), 12 (match, last char b → +3), 15 (window abxab: mismatch, b → +3 → 18 > 15, stop). That is 6 windows, of which 4 are matches; the naive method would try all 16.

More Algorithms questions

Exams that ask this topic