09 - String Matching (Solution)

# 09 - String Matching (Solution) - The Hong Kong University...

This preview shows pages 1–2. Sign up to view the full content.

COMP 271: Design and Analysis of Algorithms Fall 2007 Tutorial 9: String Matching Below is the suggested solution to the tutorial problems. Question 1 : 1. Compute the next function for the pattern P : babbabb . Show for each q how next [ q ] is used when computing next [ q + 1]. 2. Illustrate how the KMP algorithm uses next function of the previous step to ﬁnd all occurrences of P in the text T : ababbabbababbababbabb . Start by aligning P with beginning of T . Show how KMP shifts P to the right according to the next function and show the comparisons made by KMP at each shift. Solution: 1. The following shows how next[q] is computed. Note: k is the numbers of matched characters so far. Bold and red without underline means a match and bold and red with underline means a mismatch. q 1 2 3 4 5 6 7 P b a b b a b b Initially k = 0, next [1] = 0 q = 1, k = 0 b a b b a b b unmatched and k = 0, so k = 0, next [2] = k = 0 q = 2, k = 0 b a b b a

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/18/2009 for the course COMP 271 taught by Professor Arya during the Spring '07 term at HKUST.

### Page1 / 3

09 - String Matching (Solution) - The Hong Kong University...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online