1 / 7

String Matching

String Matching. 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. The Simple Algorithm. i:=1; j:=1; k:=1; While j £ T.length And k £ P.length Do If t[j] = p[k] Then

Télécharger la présentation

String Matching

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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; 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 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; Else Return False; End If

More Related