- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我检查了 KMP table-building algorithm from Wikipedia但是我不明白 while 循环的第二种情况背后的逻辑
(second case: it doesn't, but we can fall back)
else if cnd > 0 then
let cnd ← T[cnd]
我尝试使用此算法构建一个表并且它运行良好。我知道 cnd ← T[cnd]
有助于找到合适的后缀长度。我不明白的是它“如何”做到这一点?
最好有一个例子来解释。
谢谢!
编辑:我刚刚发现我的问题与这里的问题重复:"Partial match" table (aka "failure function") in KMP (on wikipedia)
我想我现在得到了答案。尽管如此,多一个解释还是有帮助的。谢谢!
最佳答案
假设你有一个字符串 Hello World!!!
并且您想搜索 Head Up
.
Hello World!!!
Head Up
^
当您处于第一个和第二个字符时,第一个条件适用 (first case: the substring continues)
,在标记位置的情况下,字符不匹配但是你已经在一个子字符串匹配中(2个字符匹配到那里),这种情况对应第二个条件(second case: it doesn't, but we can fall back)
.第三种情况是您未匹配模式的第一个字符。
第二个条件是必要的,因为你可以使用匹配字符的信息直到未匹配,以避免不必要的比较你已经知道结果(跳过你已经知道开头部分的string
的字符模式将不匹配)。
示例:使用字符串 HeHello World!!!
并搜索 Hello
HeHello World!!!
Hello
^ when you miss match this character using the table of KMP you known that
could skip 2 characters because
HeHello World!!!
Hello
^ this would miss match
在为模式 HeHello
构建模式表的情况下.假设 ^
是cnd
和 *
是pos
.起点是pos = 2
和 cnd = 0
(但是当检查模式时使用 pos - 1 = 1
)。
HeHeHello T [-1,0,0,0,0,0,0,0,0]
^* comparing 0 with 1 go to condition 3 cnd = 0, pos = 2
_
HeHeHello T [-1,0,0,1,0,0,0,0,0]
^ * comparing 0 with 2 go to condition 1 cnd = 0, pos = 3
_
HeHeHello T [-1,0,0,1,2,0,0,0,0]
^ * comparing 1 with 3 go to condition 1 cnd = 1, pos = 4
_
HeHeHello T [-1,0,0,1,2,3,0,0,0]
^ * comparing 2 with 4 go to condition 1 cnd = 2, pos = 5
_
HeHeHello T [-1,0,0,1,2,3,4,0,0]
^ * comparing 3 with 5 go to condition 1 cnd = 3, pos = 6
HeHeHello T [-1,0,0,1,2,3,4,0,0]
^ * comparing 4 with 6 go to condition 2 (cnd = T[cnd], cnd = T[4] = 2)
HeHeHello T [-1,0,0,1,2,3,4,0,0]
^ * comparing 2 with 6 go to condition 2 (cnd = T[cnd], cnd = T[2] = 0)
...
关于algorithm - KMP建表算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25769748/
KMP 算法实例详解 KMP算法,是由Knuth,Morris,Pratt共同提出的模式匹配算法,其对于任何模式和目标序列,都可以在线性时间内完成匹配查找,而不会发生退化,是一个非常优秀的模式匹配
我正在尝试使用搜索算法KMP来计算模式出现次数和所需比较(在下面的代码中称为匹配)。 我尝试执行以下操作: public class KMP { private String pat;
我想搜索一个字符串(假设a)在字符串b中出现了多少次。我想过实现 Knuth-Morris-Pratt 算法,但我更喜欢内置的 java 函数。有这样的功能吗?我希望该函数的复杂性尽可能最低,因为我多
有人要求我了解 KMP DFA,我在书中找到的是该实现,但我们的讲师一直称其为“前缀函数”。我真的不明白这个功能是哪一部分,有人可以给我解释一下吗?很抱歉,如果有人在某个地方问过这个问题,但我找不到。
我正在尝试使用 CLRS 实现 KMP 字符串匹配算法,但是文本输入为“bbaa”,模式输入为“aab”,它陷入了 while 的无限循环在 getKMPPrefix 函数中循环。我的代码如下: pr
KMP 算法在最佳情况下的最少比较次数是多少? 最佳答案 最好的情况是您要查找的字符串刚好位于文本字符串的开头。在这种情况下,如果您要在 n 字母字符串中查找 k 字母字符串,则最好的比较次数是 k。
我已经实现了用于在字符串 B 中搜索字符串 A 的 Knuth-Morris-Pratt 算法。如果找到字符串,则返回字符串的第一个位置,否则返回 -1。但是现在我想统计字符串 A 在字符串 B 中的
最近学习了KMP字符串匹配算法,差不多搞定了。但我不明白的是如何在 O( length_of_pattern ) 中构建故障函数。我不需要代码,如果可能,我需要一个清晰的解释。提前致谢! 最佳答案 来
想知道是否有人可以提供一些关于选择 KMP 和后缀树之间的利弊的建议,如果我们想看看一个字符串是否是另一个字符串的子字符串?谢谢。 提前致谢,林 最佳答案 运行时和内存复杂度大致相同。您在 O(N)
如我所见,在 KMP 中构建故障/前缀表的主要函数(在所有在线资源中,甚至在这个 answer _ 中,如下所示: int j = 0; for (int i = 1; i 0 && pa
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
扩展kmp既是求模式串和主串的每一个后缀的最长公共前缀 即令s[i]表示主串中以第i个位置为起始的后缀,则B[i]表示s[i]和模式串的最长公共前缀 显然KMP是求s[i]=模式串长度的情况,所
先声明,本人菜鸟一个,写博客是为了记录学习的过程,以及自己的理解和心得,可能有的地方写的不好,希望大神指出。。。 抛出问题 给定一个文本串test_str(被匹配的字符串)和模式串pat_str
恐怕现在用过电脑的人,一定都知道大部分带文本编辑功能的软件都有一个快捷键ctrl+f 吧(比如word)。这个功能主要来完成“查找”,“替换”和“全部替换”功能的,其实这就是典型的模式匹配的应用,即
我想统计一篇文章的词频。 我的想法是先创建一个struct数组 struct{ char[WORD_SIZE] }data[MAX_WORD_NUMBER]; 然后读取每个字符然后确
您好,我正在尝试编写 KMP search 的 C# 版本来自 C 书中的算法。无法找到我的算法中的缺陷。有人愿意帮忙吗? static int KMP(string p, string str) {
我想找到字符串 S 中与正则表达式 R 匹配的所有子字符串。正则表达式只能包含“.”和符号(其中“.”表示任何符号)。我正在尝试使用 KMP 来解决这个问题: 1) 构建字符串 T = R + '#'
从函数返回一个向量会产生什么问题吗?还是只是一些基本的语法问题? 这只是CLRS中的示例代码. computePrefix 函数计算给定模式的正确前缀的值,并匹配主函数中的值。 获取 SIGSEGV
我正在寻找一种有效的算法,以在将 pattern 与文本进行比较时允许不匹配(最多 3 个)。原始 KMP 在我的数据上有效地完成了这项工作,但正在考虑扩展该算法以适应不匹配情况。 对于我的情况:GA
我是一名优秀的程序员,十分优秀!