String Matching - PowerPoint PPT Presentation

About This Presentation
Title:

String Matching

Description:

fail[k] := k 1; End For. KMP Match Algorithm. j := 1; k := 1; While j T.Length And k P.Length Do ... k := fail[k]; End If. End While; If k P.length Then ... – PowerPoint PPT presentation

Number of Views:104
Avg rating:3.0/5.0
Slides: 8
Provided by: peterm8
Learn more at: http://cs.baylor.edu
Category:
Tags: fail | matching | string

less

Transcript and Presenter's Notes

Title: String Matching


1
String Matching
2
Statement of the Problem
  • Given a Target string T, such as ABABABCCA
  • And a Pattern string P, such as ABABC
  • Find the First Occurence of P in T

3
The Simple Algorithm
i1 j1 k1 While j T.length And k
P.length Do If tj pk Then j j
1 k k 1 Else i i 1 j
i k 1 End If End While If k gt
P.length Then Return True Else Return
False Endif
4
Better Algorithms?
  • The Simple Algorithm is Q(nm) where n and m are
    the lengths of the two strings
  • The Simple Algorithm Searches places that are
    Proven to be bad
  • Complete fore-knowledge of the pattern string is
    assumed

5
The KMP Diagram
Pattern String ABABCB
f
f
f
f
Get Next Character
s
s
s
s
s
s
s
A
B
A
B
C
B

f
1
2
3
4
5
6
7
f
0
Following s link gets next character
Refers to Target String
6
Setting The Fail Links
fail1 0 For k 2 to P.length Do r
failk-1 While r gt 0 And Pr ¹ Pk-1 Do
r failr End While failk
k1 End For
7
KMP Match Algorithm
j 1 k 1 While jT.Length And kP.Length
Do If k 0 Or Tj Pk Then j j
1 k k 1 Else k failk
End If End While If kgtP.length Then Return
True Else Return False End If
Write a Comment
User Comments (0)
About PowerShow.com