SlideServe
Menu
Préférences
© SlideServe · v2026

Knuth-Morris-Pratt Pattern Matching Algorithm

javen avatar
17 diapositives · 967 Views · Oct 14, 2014

Knuth-Morris-Pratt Pattern Matching Algorithm. Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures in C “ and “ 名題精選百則 - 使用 C 語言 ( 技巧篇 )”. Knuth-Morris-Pratt Pattern Matching Algorithm.

Knuth-Morris-Pratt Pattern Matching Algorithm - slide 1
01 / 17Knuth-Morris-Pratt Pattern Matching Algorithm
1 / 17

Télécharger la présentation

Knuth-Morris-Pratt Pattern Matching Algorithm
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Knuth-Morris-PrattPattern Matching Algorithm Instructor : Prof. Jyh-Shing Roger Jang Designer:Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures in C “ and “名題精選百則-使用C語言(技巧篇)”.

  2. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function

  3. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function First, initial f(0) = -1

  4. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function -1 First, initial f(0) = -1 j++, i = f(j-1), then compare p[i+1] and p[j] If p[i+1] != p[j] and f(i-1) == -1, put -1

  5. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function 0 1 First, initial f(0) = -1 j++, i = f(j-1), then compare p[i+1] and p[j] If p[i+1] != p[j] and f(i-1) == -1, put -1 Else if p[i+1] == p[j], put i+1 Else if p[i+1] != p[j] and i != -1, let i = f(i), until p[i+1] == p[j] or i == -1

  6. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function -1 Not equal! First, initial f(0) = -1 j++, i = f(j-1), then compare p[i+1] and p[j] If p[i+1] != p[j] and f(i-1) == -1, put -1 Else if p[i+1] == p[j], put i+1 Else if p[i+1] != p[j] and i != -1, let i = f(i), until p[i+1] == p[j] or i == -1 If i == -1 compare p[0] and p[j]

  7. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function 0 1 2 3 First, initial f(0) = -1 j++, i = f(j-1), then compare p[i+1] and p[j] If p[i+1] != p[j] and f(i-1) == -1, put -1 Else if p[i+1] == p[j], put i+1 Else if p[i+1] != p[j] and i != -1, let i = f(i), until p[i+1] == p[j] or i == -1 If i == -1 compare p[0] and p[j]

  8. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function 2 Not equal! Equal! First, initial f(0) = -1 j++, i = f(j-1), then compare p[i+1] and p[j] If p[i+1] != p[j] and f(i-1) == -1, put -1 Else if p[i+1] == p[j], put i+1 Else if p[i+1] != p[j] and i != -1, let i = f(i), until p[i+1] == p[j] or i == -1 If i == -1 compare p[0] and p[j] If p[i+1] == p[j], put i+1

  9. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function 0 Not equal! First, initial f(0) = -1 j++, i = f(j-1), then compare p[i+1] and p[j] If p[i+1] != p[j] and f(i-1) == -1, put -1 Else if p[i+1] == p[j], put i+1 Else if p[i+1] != p[j] and i != -1, let i = f(i), until p[i+1] == p[j] or i == -1 If i == -1 compare p[0] and p[j] If p[i+1] == p[j], put i+1 If i == -1 and p[0] == p[j], put i+1

  10. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function First, initial f(0) = -1 j++, i = f(j-1), then compare p[i+1] and p[j] If p[i+1] != p[j] and f(i-1) == -1, put -1 Else if p[i+1] == p[j], put i+1 Else if p[i+1] != p[j] and i != -1, let i = f(i), until p[i+1] == p[j] or i == -1 If i == -1 compare p[0] and p[j] If p[i+1] == p[j], put i+1 If i == -1 and p[0] == p[j], put i+1

  11. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function • Compare with a string s[] : Compare with s[i] and p[j], start with i = j = 0 If s[i] != p[j] and j != 0, j = f(j-1)+1

  12. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function • Compare with a string s[] : Compare with s[i] and p[j], start with i = j = 0 If s[i] != p[j] and j != 0, j = f(j-1)+1 Else if s[i] != p[j] and j == 0, i++

  13. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function • Compare with a string s[] : Compare with s[i] and p[j], start with i = j = 0 If s[i] != p[j] and j != 0, j = f(j-1)+1 Else if s[i] != p[j] and j == 0, i++

  14. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function • Compare with a string s[] : Compare with s[i] and p[j], start with i = j = 0 If s[i] != p[j] and j != 0, j = f(j-1)+1 Else if s[i] != p[j] and j == 0, i++

  15. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function • Compare with a string s[] : Compare with s[i] and p[j], start with i = j = 0 If s[i] != p[j] and j != 0, j = f(j-1)+1 Else if s[i] != p[j] and j == 0, i++

  16. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function • Compare with a string s[] : Compare with s[i] and p[j], start with i = j = 0 If s[i] != p[j] and j != 0, j = f(j-1)+1 Else if s[i] != p[j] and j == 0, i++

  17. Knuth-Morris-PrattPattern Matching Algorithm • Use failure function • Compare with a string s[] : Compare with s[i] and p[j], start with i = j = 0 If s[i] != p[j] and j != 0, j = f(j-1)+1 Else if s[i] != p[j] and j == 0, i++ Following the rules and you can finish this comparison