1 / 31

模式匹配与 KMP 算法

模式匹配与 KMP 算法. Zn http://spaces.msn.com/znzhou/. OUTLINE. 什么是模式匹配 朴素匹配算法 KMP 算法 效率对比 更多模式匹配算法. OUTLINE. 什么是模式匹配 朴素匹配算法 KMP 算法 效率对比 更多模式匹配算法. 哪个是今天要讨论的模式匹配. the The quick brown fox jumps over the lazy dog jay The quick brown fox j umps over the l a z y dog. 模式匹配.

xue
Télécharger la présentation

模式匹配与 KMP 算法

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. 模式匹配与KMP算法 Zn http://spaces.msn.com/znzhou/

  2. OUTLINE • 什么是模式匹配 • 朴素匹配算法 • KMP算法 • 效率对比 • 更多模式匹配算法

  3. OUTLINE • 什么是模式匹配 • 朴素匹配算法 • KMP算法 • 效率对比 • 更多模式匹配算法

  4. 哪个是今天要讨论的模式匹配 • the • The quick brown fox jumps over the lazy dog • jay • The quick brown fox jumps over the lazy dog

  5. 模式匹配 • Finding all occurrences of a pattern in a text • eg. 'abc' in 'acbcdabc'

  6. OUTLINE • 什么是模式匹配 • 朴素匹配算法 • KMP算法 • 效率对比 • 更多模式匹配算法

  7. The naive string-matching algorithm • ababcabcacbab • abcac • How does it work?

  8. a b a b c a b c a c b a b 第一次匹配 a b c a c a b a b c a b c a c b a b 第二次匹配 a b c a c a b a b c a b c a c b a b 第三次匹配 a b c a c

  9. a b a b c a b c a c b a b 第四次匹配 a b c a c a b a b c a b c a c b a b 第五次匹配 a b c a c a b a b c a b c a c b a b 第六次匹配 a b c a c

  10. int Normal(int pos) { int i,j; i=pos;j=0; while(s[i]!=0&&j<length) { if(s[i]==t[j]){i++;j++;} else{i=i-j+1;j=0;} } if(j==length) return i-length; else return -1; }

  11. 复杂度分析 • 一般情况 效率可以近似认为O(m+n) • 极端特殊情况 O(mn)

  12. OUTLINE • 什么是模式匹配 • 朴素匹配算法 • KMP算法 • 效率对比 • 更多模式匹配算法

  13. What’s KMP?

  14. Knuth-Morris-Pratt Professor Emeritus of The Art of Computer Programming at Stanford University, welcomes you to his home page. Donald E. Knuth,1938年出生于Wisconsin。1960年,当他毕业于Case Institute of Technology数学系时,因为成绩过于出色,被校方打破历史惯例,同时授予学士和硕士学位。他随即进入大名鼎鼎的加州理工学院数学系,仅用三年时间便取得博士学位,此时年仅25岁。 毕业后留校任助理教授,28岁时升为副教授。30岁时,加盟斯坦福大学计算机系,任正教授。从31岁那年起,他开始出版他的历史性经典巨著:The Art of Computer Programming。他计划共写7卷,然而仅仅出版三卷之后,已经震惊世界,使他获得计算机科学界的最高荣誉Turing Award!此时,他年仅38岁!后来,此书与牛顿的“自然哲学的数学原理”等一起,被评为“世界历史上最伟大的十种科学著作”之一。

  15. The KMP string-matching algorithm • abbcaccabbaabcababcdbac • abcd • How does it work?

  16. a b a b c a b c a c b a b 第一次匹配 a b c a c a b a b c a b c a c b a b 第二次匹配 a b c a c a b a b c a b c a c b a b 第三次匹配 a b c a c

  17. Next[j] a b a b c a b c a c b a b a b c a c a b c a c a b c a c

  18. int KMP(char*t,int pos) { int i,j; i=pos; j=0; while(s[i]!=0&&j<length) { if(j==-1||t[j]==s[i]){i++;j++;} else{j=next[j];} } if(j==length) return i-j; else return -1; }

  19. 复杂度分析 O(m+n)

  20. How to gain next[j]?

  21. 以眼杀人--观察法 a a b a c a c b a b a a b c a c -1 0 0 1 0 1 2 1

  22. Exercise • a b c a b a a b c b c -1 0 0 0 1 2 1 1 2 0 0 • a b a b a b a b -1 0 0 1 2 3 4 5 • a a b a a c a a d a -1 0 1 0 1 2 0 1 2 0

  23. 程序实现 S j a b a a b c a c T i Next[i] -1 0 0 1 0 1 2 1 If(j==-1||s[j]==t[i]) i++;j++;next[i]=j; Else j=next[j]

  24. void CalcNext(char*t) { int i,j; i=0; next[0]=-1; j=-1; while(i<length-1) { if(j==-1||t[i]==t[j]){i++;j++;next[i]=j;} else{j=next[j];} } }

  25. OUTLINE • 什么是模式匹配 • 朴素匹配算法 • KMP算法 • 效率对比 • 更多模式匹配算法

  26. 朴素算法与KMP算法的比较 • 复杂度 • 使用资源 • 效率

  27. KMP算法的改进 • 一个例子 模式串:aaaab 主串: aaabaaaab

  28. OUTLINE • 什么是模式匹配 • 朴素匹配算法 • KMP算法 • 效率对比 • 更多模式匹配算法

  29. 更多模式匹配算法 • Boyer-Moore算法 这个算法KMP算法的不同点是在作s[k+1..k+m]与t[1..m]的匹配测试时是从右到左,而不是从左到右。 • Rabin-Karp算法这个算法用到数论中诸如两个整数关于第三个整数取模的等价性等初等概念。

  30. SUMMARY • 什么是模式匹配 • 朴素匹配算法 • KMP算法 • 效率对比 • 更多模式匹配算法

  31. THANK YOU! Zn http://spaces.msn.com/znzhou/

More Related