Next: , Up: kmp   [Index]


29.1 Introduction

When searching for a pattern string in a text string:

(define pattern "ciao")
(define text    "Oh, ciao!")

we start looking for the first char in the pattern:

(define match-start
  (let loop ((i 0))
    (if (char=? (string-ref text i)
                (string-ref pattern 0))
        i
      (loop (+ 1 i)))))
⇒ 3

now that we know a match for the first char, we go on looking for the full pattern:

(define match-past
  (let loop ((pi 1)                     ;; pattern index
             (ti (+ 1 match-start)))    ;; text index
    (cond
     ((= pi (string-length pattern)) pi)    ;; found
     ((= ti (string-length text))    #f)    ;; not found
     (else
      (if (char=? (string-ref ti text)
                  (string-ref pi pattern))
          (loop (+ 1 pi) (+ 1 ti))
        #f)))))                             ;; not found

we have the following possible outcomes:

The action “restart matching” involves backtracking (to go back) in the text string; this can be avoided if we do the following considerations.

Summary: We can implement an efficient algorithm if we precompute where to restart from, after a partial match. Of course, since the algorithm needs no backtracking, we can also search for multiple patterns in “parallel”.


Next: , Up: kmp   [Index]