博客
关于我
Objective-C实现Boyer-Moore字符串搜索算法(附完整源码)
阅读量:794 次
发布时间: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/

你可能感兴趣的文章
NodeJs学习笔记001--npm换源
查看>>
Nodejs教程09:实现一个带接口请求的简单服务器
查看>>
Nodejs简介以及Windows上安装Nodejs
查看>>
nodejs系列之express
查看>>
Nodejs连接mysql
查看>>
nodejs连接mysql
查看>>
nodejs配置express服务器,运行自动打开浏览器
查看>>
Node中的Http模块和Url模块的使用
查看>>
Node入门之创建第一个HelloNode
查看>>
node全局对象 文件系统
查看>>
Node出错导致运行崩溃的解决方案
查看>>
node基础(二)_模块以及处理乱码问题
查看>>
node安装及配置之windows版
查看>>
Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
查看>>
Node读取并输出txt文件内容
查看>>
node防xss攻击插件
查看>>
noi 7827 质数的和与积
查看>>
NOIp2005 过河
查看>>
NOIp模拟赛二十九
查看>>
NOPI读取Excel
查看>>