|
1 | 1 | # 动态规划之KMP字符匹配算法
|
2 | 2 |
|
| 3 | + |
| 4 | +<p align='center'> |
| 5 | +<a href="https://github.com/labuladong/fucking-algorithm" target="view_window"><img alt="GitHub" src="https://img.shields.io/github/stars/labuladong/fucking-algorithm?label=Stars&style=flat-square&logo=GitHub"></a> |
| 6 | +<a href="https://www.zhihu.com/people/labuladong"><img src="https://img.shields.io/badge/%E7%9F%A5%E4%B9%8E-@labuladong-000000.svg?style=flat-square&logo=Zhihu"></a> |
| 7 | +<a href="https://i.loli.net/2020/10/10/MhRTyUKfXZOlQYN.jpg"><img src="https://img.shields.io/badge/公众号-@labuladong-000000.svg?style=flat-square&logo=WeChat"></a> |
| 8 | +<a href="https://space.bilibili.com/14089380"><img src="https://img.shields.io/badge/B站-@labuladong-000000.svg?style=flat-square&logo=Bilibili"></a> |
| 9 | +</p> |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +相关推荐: |
| 14 | + * [经典动态规划:最长公共子序列](https://labuladong.gitbook.io/algo) |
| 15 | + * [特殊数据结构:单调栈](https://labuladong.gitbook.io/algo) |
| 16 | + |
| 17 | +读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: |
| 18 | + |
| 19 | +[28.实现 strStr()](https://leetcode-cn.com/problems/implement-strstr) |
| 20 | + |
| 21 | +**-----------** |
| 22 | + |
3 | 23 | KMP 算法(Knuth-Morris-Pratt 算法)是一个著名的字符串匹配算法,效率很高,但是确实有点复杂。
|
4 | 24 |
|
5 | 25 | 很多读者抱怨 KMP 算法无法理解,这很正常,想到大学教材上关于 KMP 算法的讲解,也不知道有多少未来的 Knuth、Morris、Pratt 被提前劝退了。有一些优秀的同学通过手推 KMP 算法的过程来辅助理解该算法,这是一种办法,不过本文要从逻辑层面帮助读者理解算法的原理。十行代码之间,KMP 灰飞烟灭。
|
@@ -37,7 +57,7 @@ int search(String pat, String txt) {
|
37 | 57 | }
|
38 | 58 | ```
|
39 | 59 |
|
40 |
| -对于暴力算法,如果出现不匹配字符,同时回退 `txt` 和 `pat` 的指针,嵌套 for 循环,时间复杂度 $O(MN)$,空间复杂度$O(1)$。最主要的问题是,如果字符串中重复的字符比较多,该算法就显得很蠢。 |
| 60 | +对于暴力算法,如果出现不匹配字符,同时回退 `txt` 和 `pat` 的指针,嵌套 for 循环,时间复杂度 `O(MN)`,空间复杂度`O(1)`。最主要的问题是,如果字符串中重复的字符比较多,该算法就显得很蠢。 |
41 | 61 |
|
42 | 62 | 比如 txt = "aaacaaab" pat = "aaab":
|
43 | 63 |
|
@@ -399,12 +419,14 @@ public class KMP {
|
399 | 419 |
|
400 | 420 | KMP 算法也就是动态规划那点事,我们的公众号文章目录有一系列专门讲动态规划的,而且都是按照一套框架来的,无非就是描述问题逻辑,明确 `dp` 数组含义,定义 base case 这点破事。希望这篇文章能让大家对动态规划有更深的理解。
|
401 | 421 |
|
402 |
| -**致力于把算法讲清楚!欢迎关注我的微信公众号 labuladong,查看更多通俗易懂的文章**: |
403 | 422 |
|
404 |
| - |
405 | 423 |
|
406 |
| -[上一篇:贪心算法之区间调度问题](../动态规划系列/贪心算法之区间调度问题.md) |
| 424 | +**_____________** |
| 425 | + |
| 426 | +**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 |
407 | 427 |
|
408 |
| -[下一篇:团灭 LeetCode 股票买卖问题](../动态规划系列/团灭股票问题.md) |
| 428 | +**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 |
409 | 429 |
|
410 |
| -[目录](../README.md#目录) |
| 430 | +<p align='center'> |
| 431 | +<img src="../pictures/table_qr2.jpg" width=500 > |
| 432 | +</p> |
0 commit comments