字符串搜索算法Boyer-Moore
整理日: 2015年2月16日
1. 主要特征
假设文本串text长度为n,模式串pattern长度为m,BM算法的主要特征为:
- 从右往左进行比较匹配(一般的字符串搜索算法如KMP都是从从左往右进行匹配);
- 算法分为两个阶段:预处理阶段和搜索阶段;
- 预处理阶段时间和空间复杂度都是是O(m+sigma),sigma是字符集大小,一般为256;
- 搜索阶段时间复杂度是O(mn);
- 当模式串是非周期性的,在最坏的情况下算法需要进行3n次字符比较操作;
- 算法在最好的情况下达到O(n / m),比如在文本串bn中搜索模式串am-1b ,只需要n/m次比较。
2. 算法基本思想
常规的匹配算法移动模式串的时候是从左到右,而进行比较的时候也是从左到右的,基本框架是:
while(j <= strlen(text) - strlen(pattern)){
for (i = 0; i < strlen(pattern) && pattern[i] == text[i + j]; ++i);
if (i == strlen(pattern)) {
Match;
break;
}
else
++j;
}
而BM算法在移动模式串的时候是从左到右,而进行比较的时候是从右到左的,基本框架是:
while(j <= strlen(text) - strlen(pattern)){
for (i = strlen(pattern); i >= 0 && pattern[i] == text[i + j]; --i);
if (i < 0)) {
Match;
break;
}
else
j += BM();
}
3. 实例:算法整合
#include <stdio.h>
#include <string.h>
#define MAX_CHAR 256
#define SIZE 256
#define MAX(x, y) (x) > (y) ? (x) : (y)
void BoyerMoore(char *pattern, int m, char *text, int n);
int main()
{
char text[256], pattern[256];
while(1)
{
scanf("%s%s", text, pattern);
if(text == 0 || pattern == 0) break;
BoyerMoore(pattern, strlen(pattern), text, strlen(text));
printf("\n");
}
return 0;
}
void print(int *array, int n, char *arrayName)
{
int i;
printf("%s: ", arrayName);
for(i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
void PreBmBc(char *pattern, int m, int bmBc[])
{
int i;
for(i = 0; i < MAX_CHAR; i++)
{
bmBc[i] = m;
}
for(i = 0; i < m - 1; i++)
{
bmBc[pattern[i]] = m - 1 - i;
}
/* printf("bmBc[]: ");
for(i = 0; i < m; i++)
{
printf("%d ", bmBc[pattern[i]]);
}
printf("\n"); */
}
void suffix_old(char *pattern, int m, int suff[])
{
int i, j;
suff[m - 1] = m;
for(i = m - 2; i >= 0; i--)
{
j = i;
while(j >= 0 && pattern[j] == pattern[m - 1 - i + j]) j--;
suff[i] = i - j;
}
}
void suffix(char *pattern, int m, int suff[]) {
int f, g, i;
suff[m - 1] = m;
g = m - 1;
for (i = m - 2; i >= 0; --i) {
if (i > g && suff[i + m - 1 - f] < i - g)
suff[i] = suff[i + m - 1 - f];
else {
if (i < g)
g = i;
f = i;
while (g >= 0 && pattern[g] == pattern[g + m - 1 - f])
--g;
suff[i] = f - g;
}
}
// print(suff, m, "suff[]");
}
void PreBmGs(char *pattern, int m, int bmGs[])
{
int i, j;
int suff[SIZE];
// 计算后缀数组
suffix(pattern, m, suff);
// 先全部赋值为m,包含Case3
for(i = 0; i < m; i++)
{
bmGs[i] = m;
}
// Case2
j = 0;
for(i = m - 1; i >= 0; i--)
{
if(suff[i] == i + 1)
{
for(; j < m - 1 - i; j++)
{
if(bmGs[j] == m)
bmGs[j] = m - 1 - i;
}
}
}
// Case1
for(i = 0; i <= m - 2; i++)
{
bmGs[m - 1 - suff[i]] = m - 1 - i;
}
// print(bmGs, m, "bmGs[]");
}
void BoyerMoore(char *pattern, int m, char *text, int n)
{
int i, j, bmBc[MAX_CHAR], bmGs[SIZE];
// Preprocessing
PreBmBc(pattern, m, bmBc);
PreBmGs(pattern, m, bmGs);
// Searching
j = 0;
while(j <= n - m)
{
for(i = m - 1; i >= 0 && pattern[i] == text[i + j]; i--);
if(i < 0)
{
printf("Find it, the position is %d\n", j);
j += bmGs[0];
return;
}
else
{
j += MAX(bmBc[text[i + j]] - m + 1 + i, bmGs[i]);
}
}
printf("No find.\n");
}
字符串搜索算法Boyer-Moore的更多相关文章
- Boyer–Moore (BM)字符串搜索算法
在计算机科学里,Boyer-Moore字符串搜索算法是一种非常高效的字符串搜索算法.它由Bob Boyer和J Strother Moore设计于1977年.此算法仅对搜索目标字符串(关键字)进行预处 ...
- grep之字符串搜索算法Boyer-Moore由浅入深(比KMP快3-5倍)
这篇长文历时近两天终于完成了,前两天帮网站翻译一篇文章“为什么GNU grep如此之快?”,里面提及到grep速度快的一个重要原因是使用了Boyer-Moore算法作为字符串搜索算法,兴趣之下就想了解 ...
- grep之字符串搜索算法Boyer-Moore由浅入深(比KMP快3-5倍)(转)
这篇长文历时近两天终于完成了,前两天帮网站翻译一篇文章“为什么GNU grep如此之快?”,里面提及到grep速度快的一个重要原因是使用了Boyer-Moore算法作为字符串搜索算法,兴趣之下就想了解 ...
- 从入门到精通之Boyer-Moore字符串搜索算法详解
本文讲述的是Boyer-Moore算法,Boyer-Moore算法作为字符串搜索算法,兴趣之下就想了解这个算法,发现这个算法一开始还挺难理解的,也许是我理解能力不是很好吧,花了小半天才看懂,看懂了过后 ...
- Boyer Moore算法(字符串匹配)
上一篇文章,我介绍了KMP算法. 但是,它并不是效率最高的算法,实际采用并不多.各种文本编辑器的"查找"功能(Ctrl+F),大多采用Boyer-Moore算法. Boyer-Mo ...
- Leetcode OJ : Implement strStr() [ Boyer–Moore string search algorithm ] python solution
class Solution { public: int strStr(char *haystack, char *needle) { , skip[]; char *str = haystack, ...
- BF + KMP + BM 字符串搜索算法
BF #include <stdio.h> #include <string.h> int simplicity(char *s, char *t, int pos); int ...
- Google Interview University - 坚持完成这套学习手册,你就可以去 Google 面试了
作者:Glowin链接:https://zhuanlan.zhihu.com/p/22881223来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 原文地址:Google ...
- 字符串核对之Boyer-Moore算法
算法说明: 在计算机科学里,Boyer-Moore字符串搜索算法是一种非常高效的字符串搜索算法.它由Bob Boyer和J Strother Moore设计于1977年.此算法仅对搜索目标字符串(关键 ...
随机推荐
- android考试题
一.选择题 1. Math.round(11.5)等于多少( ). Math.round(-11.5) 等于多少( C ). A.11 ,-11 B.11 ,-12 C.12 ,-1 ...
- 解析xlsx文件---Java读取Excel2007
关于Java读取Excel2007的文章在Google.百度上搜索一下,没有太好的例子,实现的也不算太好.查看了一下Poi,最新的 POI 3.5 beta 4 支持读写 Excel2007和PPT2 ...
- poj 3294 Life Forms
后缀数组的题目,把后缀连接起来,这个还是先二分答案,然后选取一段连续的height值,判断这些height代表的后缀有没有覆盖一半以上的字符串. 得出答案的长度之后还要在枚举连续的heigh,判断有没 ...
- hadoop错误DataXceiver error processing WRITE_BLOCK operation
错误: DataXceiver error processing WRITE_BLOCK operation 原因: 文件操作超租期,实际上就是data stream操作过程中文件被删掉了. ...
- 剖析@weakify 和 @strongify
前言 使用RAC的时候我们常会看到这两个宏@weakify(self).@strongify(self),用来防止使用block时出现引用闭环. 今天看YYKit的时候,看到里面也写了类似的宏,还是来 ...
- Windows Server 2008 R2 安装及配置指南
一.安装需求: 1. 硬件需求条件 硬件 需求 处理器 最低:1.4 GHz(x64处理器) 注意:Windows Server 2008 for Itanium-Based Systems 版本需要 ...
- <div>相关
定义 <div>是一个块级元素[会自动换行] 用法 <div>可用于划分独立的一个块状区域,其内部内容显示在<div>的content部分内 结构 [盗用张图] 从 ...
- php strtotime函数服务器和本地不相同
遇到过一种情况strtotime 在本地和服务器不相同 服务器返回的是-1 strtotime($sa_sagyo_ymd."23:59:59") 如果这样用不了,就只能换一种写法 ...
- PHP ajax实现数组返回
首先,我想要实这样一个功能, 当选择一个下拉框时,让其它三个文本框得到从服务器上返回的值!也就把返回的值,赋给那三个文本框! 我用的是jquery+php!! 由于我前台,后台,js,数据库采用的都是 ...
- ASP.Net免费发送短信-阿里大鱼短信接口
有点短信余额 不用白不用 3月1号就过期了 情人节做了个免费发短信的 http://love.issuc.com/ 固定短信模板 [活动验证]您正在参加XXX的OOO活动,请确认系本人申请.需要的可以 ...