有限状态机之 KMP 字符匹配算法 :: labuladong的算法小抄 #1036
Replies: 25 comments 4 replies
-
影子状态这个名字好!X 与 j 就是如影随形的关系。 |
Beta Was this translation helpful? Give feedback.
-
盯着GIF看了半天,差点晕了😄 |
Beta Was this translation helpful? Give feedback.
-
搞定影子状态更新,就是一个标准动规,这个动规求得东西只是个辅助工具。是大问题的子问题,那三个元祖是真的猛啊。。 |
Beta Was this translation helpful? Give feedback.
-
提个小建议 |
Beta Was this translation helpful? Give feedback.
-
万一字符串含有中文呢 |
Beta Was this translation helpful? Give feedback.
-
如果想自己跟着代码走看流程的话,可以给算法精简一下只处理 a..i 之间的字符, pat.charAt 的时候 - 'a' 这样方便些 |
Beta Was this translation helpful? Give feedback.
-
「影子状态」这段其实讲得不好,这里有更清楚的描述:https://juejin.cn/post/6856374004421722125
|
Beta Was this translation helpful? Give feedback.
-
影子状态这个挺难的 |
Beta Was this translation helpful? Give feedback.
-
赞一个!写得很清楚,终于给整明白了 |
Beta Was this translation helpful? Give feedback.
-
check in |
Beta Was this translation helpful? Give feedback.
-
这个章节修改了,又来看了一遍. |
Beta Was this translation helpful? Give feedback.
-
由于X和j一开始相差了1,所以X和j一定不会相遇(最接近的情况是一直有连续重复的字符,比如'aaaaa',此时X和j始终相距1),而X的状态推进逻辑是,当dp[X][pat.charAt(j)] = X+1时,X才会推进(即X=dp[X][pat.charAt(j)]=X+1),而根据之前的dp定义,只有dp[X][pat.charAt(X)] 时才等于 X+1,可以得到pat.charAt(j) === pat.charAt(X),即只有X位置的字符和j位置的字符相同时,X和j才会同时推进,此时如果同时开始推进,就代表【k |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
把gif转为图片序列看就不会晃眼了 |
Beta Was this translation helpful? Give feedback.
-
还是没有理解影子状态是如何得到的。。 |
Beta Was this translation helpful? Give feedback.
-
东哥,以后能不能把gif改成那种点一下前进一步的样式,这样自己跳,也跟不上啊! |
Beta Was this translation helpful? Give feedback.
-
打半个卡 |
Beta Was this translation helpful? Give feedback.
-
关于影子状态,理论上给来说,如果pat存在多种影子,那么应该在程序中增加影子1/影子2/影子3,从而优化算法. |
Beta Was this translation helpful? Give feedback.
-
谢谢拉哥,之前看KMP一直不懂,终于解决了一直困扰我的一个难题,哈哈 |
Beta Was this translation helpful? Give feedback.
-
看了一天也没看懂影子状态,感觉我没救了 |
Beta Was this translation helpful? Give feedback.
-
swift 版本
|
Beta Was this translation helpful? Give feedback.
-
影子状态不太容易弄懂,需要跟着例子在脑子里过几遍。 |
Beta Was this translation helpful? Give feedback.
-
应该是在pat[1:end]中找pat的第一次出现吧 |
Beta Was this translation helpful? Give feedback.
-
文章链接点这里:有限状态机之 KMP 字符匹配算法
评论礼仪 见这里,违者直接拉黑。
Beta Was this translation helpful? Give feedback.
All reactions