Presentation is loading. Please wait.

Presentation is loading. Please wait.

String Matching.

Similar presentations


Presentation on theme: "String Matching."— Presentation transcript:

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 i:=1; j:=1; k:=1;
While j £ T.length And k £ P.length Do If t[j] = p[k] Then j := j + 1; k := k + 1; Else i := i + 1; j := i; k := 1; End If End While; If k > P.length Then Return True; 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
* 1 2 3 f 4 5 6 7 f Following “s” link gets next character Refers to Target String

6 Setting The Fail Links fail[1] := 0; For k := 2 to P.length Do
r := fail[k-1]; While r > 0 And P[r] ¹ P[k-1] Do r := fail[r]; End While; fail[k] := k+1; End For

7 KMP Match Algorithm j := 1; k := 1; While j£T.Length And k£P.Length Do
If k = 0 Or T[j] = P[k] Then j := j + 1; k := k + 1; Else k := fail[k]; End If End While; If k>P.length Then Return True; Return False;


Download ppt "String Matching."

Similar presentations


Ads by Google