143k views
5 votes
Exercise 1: (Brute Force: String Matching)

How many comparison (both successful and unsuccessful) are made by the brute-force string-matching algorithm in searching for each of the following patterns in the binary text of 1000 zeros?
a. 00001
b. 10000
c. 01010

User Obautista
by
7.2k points

1 Answer

2 votes

Final answer:

The brute-force string-matching algorithm requires 996 comparisons for each pattern.

Step-by-step explanation:

Brute Force String Matching Algorithm

The brute-force string-matching algorithm compares the given pattern with every substring of the binary text until a match is found. Let's analyze the number of comparisons for each pattern:

  1. a. 00001: The pattern '00001' has a length of 5. In the worst case, we need to compare each character of the binary text with the pattern. Since the binary text has 1000 zeros, it would require 1000 - 5 + 1 = 996 comparisons.
  2. b. 10000: The pattern '10000' also has a length of 5. Similarly, it would require 996 comparisons.
  3. c. 01010: The pattern '01010' has a length of 5 as well. Again, it would require 996 comparisons.

Therefore, regardless of the pattern, we would need 996 comparisons for each of them.

User Daniel Bang
by
8.0k points