博客
关于我
Objective-C实现Boyer-Moore字符串搜索算法(附完整源码)
阅读量:797 次
发布时间:2023-02-17

本文共 4741 字,大约阅读时间需要 15 分钟。

Objective-C??Boyer-Moore???????

Objective-C??Boyer-Moore???????

Boyer-Moore????????????????????????????????????????????????????????????????????????????

#include <foundation/foundation.h>

#define ALPHABET_SIZE 256

#endif

????Objective-C???Boyer-Moore?????????????

#import <foundation/foundation.h>

#define ALPHABET_SIZE 256

#if DEBUG#define LOG(x) NSLog(x)#else#define LOG(x) ((void)0)#endif

typedef struct {unsigned char *pattern;unsigned char *text;size_t patternLength;size_t textLength;size_t remaining;unsigned char badChar;unsigned char goodSuffix;unsigned char lastMatch;unsigned char currentMatch;unsigned char maxMatch;unsigned char maxPatternLength;unsigned char maxTextLength;unsigned char maxBadChar;unsigned char maxGoodSuffix;unsigned char maxMaxMatch;} BoyerMooreContext;

static BoyerMooreContext *alcBoyerMoore(void *text, const unsigned char *pattern, size_t patternLength, size_t textLength) {BoyerMooreContext *context = malloc(sizeof(BoyerMooreContext));context->pattern = pattern;context->text = text;context->patternLength = patternLength;context->textLength = textLength;context->remaining = patternLength;context->badChar = 0;context->goodSuffix = 0;context->lastMatch = 0;context->currentMatch = 0;context->maxMatch = 0;context->maxPatternLength = patternLength;context->maxTextLength = textLength;context->maxBadChar = 0;context->maxGoodSuffix = 0;context->maxMaxMatch = 0;return context;}

static void deallocBoyerMoore(BoyerMooreContext *context) {free(context->pattern);free(context->text);free(context);}

static size_t boyermooreSearch(BoyerMooreContext *context) {unsigned char *text = context->text;size_t textLength = context->textLength;size_t patternLength = context->patternLength;size_t remaining = context->remaining;

size_t i = textLength;unsigned char *p = context->pattern;unsigned char *t = text;unsigned char badChar = context->badChar;unsigned char goodSuffix = context->goodSuffix;unsigned char lastMatch = context->lastMatch;unsigned char currentMatch = context->currentMatch;unsigned char maxMatch = context->maxMatch;unsigned char maxPatternLength = context->maxPatternLength;unsigned char maxTextLength = context->maxTextLength;unsigned char maxBadChar = context->maxBadChar;unsigned char maxGoodSuffix = context->maxGoodSuffix;unsigned char maxMaxMatch = context->maxMaxMatch;while (remaining > 0) {    if (t[i] == p[0]) {        size_t matched = 0;        do {            matched++;            if (matched > maxMatch) break;            if (t[i + matched] != p[matched]) break;            i += matched;            remaining -= matched;            if (matched > maxMaxMatch) maxMaxMatch = matched;        } while (matched <= patternLength && i + matched < textLength);        if (matched > 0) {            lastMatch = currentMatch;            currentMatch = matched;            if (currentMatch > maxMatch) maxMatch = currentMatch;            if (currentMatch == patternLength) {                LOG("Pattern found at position %ld", i - patternLength);                return i - patternLength;            }            if (currentMatch > maxMaxMatch) maxMaxMatch = currentMatch;        } else {            if (i > 0) {                if (t[i] == badChar || (goodSuffix != 0 && t[i] == goodSuffix)) {                    i--;                    remaining = patternLength;                    lastMatch = 0;                    currentMatch = 0;                    maxMatch = 0;                    maxMaxMatch = 0;                } else {                    i--;                    remaining = patternLength;                    lastMatch = 0;                    currentMatch = 0;                    maxMatch = 0;                    maxMaxMatch = 0;                }            } else {                i = textLength;                remaining = patternLength;                lastMatch = 0;                currentMatch = 0;                maxMatch = 0;                maxMaxMatch = 0;            }        }    } else {        if (i > 0) {            if (t[i] == badChar || (goodSuffix != 0 && t[i] == goodSuffix)) {                i--;                remaining = patternLength;                lastMatch = 0;                currentMatch = 0;                maxMatch = 0;                maxMaxMatch = 0;            } else {                i--;                remaining = patternLength;                lastMatch = 0;                currentMatch = 0;                maxMatch = 0;                maxMaxMatch = 0;            }        } else {            i = textLength;            remaining = patternLength;            lastMatch = 0;            currentMatch = 0;            maxMatch = 0;            maxMaxMatch = 0;        }    }}return -1;

}

#endif

转载地址:http://ccnfk.baihongyu.com/

你可能感兴趣的文章
Objective-C实现bellmanFord贝尔曼-福特算法(附完整源码)
查看>>
Objective-C实现BellmanFord贝尔曼-福特算法(附完整源码)
查看>>
Objective-C实现bezier curve贝塞尔曲线算法(附完整源码)
查看>>
Objective-C实现bfs 最短路径算法(附完整源码)
查看>>
Objective-C实现BF算法 (附完整源码)
查看>>
Objective-C实现Bilateral Filter双边滤波器算法(附完整源码)
查看>>
Objective-C实现binary exponentiation二进制幂运算算法(附完整源码)
查看>>
Objective-C实现binary search二分查找算法(附完整源码)
查看>>
Objective-C实现binary tree mirror二叉树镜像算法(附完整源码)
查看>>
Objective-C实现binary tree traversal二叉树遍历算法(附完整源码)
查看>>
Objective-C实现BinarySearchTreeNode树算法(附完整源码)
查看>>
Objective-C实现binarySearch二分查找算法(附完整源码)
查看>>
Objective-C实现binomial coefficient二项式系数算法(附完整源码)
查看>>
Objective-C实现binomial distribution二项分布算法(附完整源码)
查看>>
Objective-C实现bisection二分法算法(附完整源码)
查看>>
Objective-C实现bisection二等分算法(附完整源码)
查看>>
Objective-C实现BitMap算法(附完整源码)
查看>>
Objective-C实现bitmask位掩码算法(附完整源码)
查看>>
Objective-C实现bitonic sort双调排序算法(附完整源码)
查看>>
Objective-C实现BloomFilter布隆过滤器的算法(附完整源码)
查看>>