ProgrammingVery hard

A naive strstr-style search looks for a pattern of length m = 50000 in a text of length n = 100000. The text is all 'a' and the pattern is 'a' repeated m-1 times followed by 'b', so there is never a match. Roughly how many character comparisons does the search make?

  1. A. About 10^5
  2. B. About 5 x 10^4
  3. C. About 2.5 x 10^9
  4. D. About 10^10

Correct answer: C. About 2.5 x 10^9

The naive method tries each of the n - m + 1 starting positions, and here it compares nearly all m characters before failing at each one. So the work is about (n - m) x m, roughly 2.5 x 10^9.

More C Programming questions

Exams that ask this topic