C 中是否有 Boyer-Moore 字符串搜索算法的工作示例?我查看了一些网站,但它们看起来很糟糕,包括维基百科。
谢谢。
子字符串搜索算法的最佳站点:
Bob Stout 的Snippets网站上有几个 Boyer-Moore-Horspool 的实现(包括周日的变体) 。据我所知, Ray Gardner 在BMHSRCH.C中的实现没有错误1 ,而且绝对是我见过或听说过的最快的。然而,这并不是最容易理解的——他使用了一些相当棘手的代码来使内部循环尽可能简单。我可能有偏见,但我认为我在PBMSRCH.C中的第2版更容易理解(尽管肯定会慢一些)。
1在其限制范围内——它最初是为 MS-DOS 编写的,并且可以为提供更多内存的环境使用重写。
2这不知何故被贴上了“Pratt-Boyer-Moore”的标签,但实际上是周日的 Boyer-Moore-Horspool 的变体(虽然我当时不知道也没有发布它,但我相信我实际上是发明了它大约比星期天早一年)。
这是我用许多奇怪的测试用例强调的 C90 实现:
#ifndef MAX
#define MAX(a,b) ((a > b) ? (a) : (b))
#endif
void fillBadCharIndexTable (
/*----------------------------------------------------------------
function:
the table fits for 8 bit character only (including utf-8)
parameters: */
size_t aBadCharIndexTable [],
char const * const pPattern,
size_t const patternLength)
/*----------------------------------------------------------------*/
{
size_t i;
size_t remainingPatternLength = patternLength - 1;
for (i = 0; i < 256; ++i) {
aBadCharIndexTable [i] = patternLength;
}
for (i = 0; i < patternLength; ++i) {
aBadCharIndexTable [pPattern [i]] = remainingPatternLength--;
}
}
void fillGoodSuffixRuleTable (
/*----------------------------------------------------------------
function:
the table fits for patterns of length < 256; for longer patterns ... (1 of)
- increase the static size
- use variable length arrays and >= C99 compilers
- allocate (and finally release) heap according to demand
parameters: */
size_t aGoodSuffixIndexTable [],
char const * const pPattern,
size_t const patternLength)
/*----------------------------------------------------------------*/
{
size_t const highestPatternIndex = patternLength - 1;
size_t prefixLength = 1;
/* complementary prefix length, i.e. difference from highest possible pattern index and prefix length */
size_t cplPrefixLength = highestPatternIndex;
/* complementary length of recently inspected pattern substring which is simultaneously pattern prefix and suffix */
size_t cplPrefixSuffixLength = patternLength;
/* too hard to explain in a C source ;-) */
size_t iRepeatedSuffixMax;
aGoodSuffixIndexTable [cplPrefixLength] = patternLength;
while (cplPrefixLength > 0) {
if (!strncmp (pPattern, pPattern + cplPrefixLength, prefixLength)) {
cplPrefixSuffixLength = cplPrefixLength;
}
aGoodSuffixIndexTable [--cplPrefixLength] = cplPrefixSuffixLength + prefixLength++;
}
if (pPattern [0] != pPattern [highestPatternIndex]) {
aGoodSuffixIndexTable [highestPatternIndex] = highestPatternIndex;
}
for (iRepeatedSuffixMax = 1; iRepeatedSuffixMax < highestPatternIndex; ++iRepeatedSuffixMax) {
size_t iSuffix = highestPatternIndex;
size_t iRepeatedSuffix = iRepeatedSuffixMax;
do {
if (pPattern [iRepeatedSuffix] != pPattern [iSuffix]) {
aGoodSuffixIndexTable [iSuffix] = highestPatternIndex - iRepeatedSuffix;
break;
}
--iSuffix;
} while (--iRepeatedSuffix > 0);
}
}
char const * boyerMoore (
/*----------------------------------------------------------------
function:
find a pattern (needle) inside a text (haystack)
parameters: */
char const * const pHaystack,
size_t const haystackLength,
char const * const pPattern)
/*----------------------------------------------------------------*/
{
size_t const patternLength = strlen (pPattern);
size_t const highestPatternIndex = patternLength - 1;
size_t aBadCharIndexTable [256];
size_t aGoodSuffixIndexTable [256];
if (*pPattern == '\0') {
return pHaystack;
}
if (patternLength <= 1) {
return strchr (pHaystack, *pPattern);
}
if (patternLength >= sizeof aGoodSuffixIndexTable) {
/* exit for too long patterns */
return 0;
}
{
char const * pInHaystack = pHaystack + highestPatternIndex;
/* search preparation */
fillBadCharIndexTable (
aBadCharIndexTable,
pPattern,
patternLength);
fillGoodSuffixRuleTable (
aGoodSuffixIndexTable,
pPattern,
patternLength);
/* search execution */
while (pInHaystack++ < pHaystack + haystackLength) {
int iPattern = (int) highestPatternIndex;
while (*--pInHaystack == pPattern [iPattern]) {
if (--iPattern < 0) {
return pInHaystack;
}
}
pInHaystack += MAX (aBadCharIndexTable [*pInHaystack], aGoodSuffixIndexTable [iPattern]);
}
}
return 0;
}