算法说明:

在计算机科学里,Boyer-Moore字符串搜索算法是一种非常高效的字符串搜索算法。它由Bob Boyer和J Strother Moore设计于1977年。此算法仅对搜索目标字符串(关键字)进行预处理,而非被搜索的字符串。虽然Boyer-Moore算法的执行时间同样线性依赖于被搜索字符串的大小,但是通常仅为其它算法的一小部分:它不需要对被搜索的字符串中的字符进行逐一比较,而会跳过其中某些部分。通常搜索关键字越长,算法速度越快。它的效率来自于这样的事实:对于每一次失败的匹配尝试,算法都能够使用这些信息来排除尽可能多的无法匹配的位置。

算法原理:

假设被检索文字列是“1234567890”,检索文字列是“MOORE”。简单的比较需要执行十次才得到结论不匹配。

被检索文字列:1234567890第一次比较:M....         (M和1比较,不匹配)第二次比较: M....        (M和2比较,不匹配)第三次比较:  M....       (M和3比较,不匹配)...第十次比较:         M....(M和0比较,不匹配)

※未参与比较的文字用【.】占位。

BM算法只需要2次比较。

被检索文字列:1234567890第一次比较:....E        (E和5比较,不匹配,并且5不是MOORE中任何文字)第二次比较:     ....E   (E和0比较,不匹配,并且0不是MOORE中任何文字)

第一次从检索文字的末尾开始,因为如果被检索文字的第5文字位置不是E,则无论前4个文字是什么,都绝不可能匹配了。这一点比较容易理解。 那么,为什么不用E和6比较呢?

这是BM算法又一处精妙之处。在E和5进行比较的时候不仅知道他们不相等,而且还知道了5不和检索文字MOORE中的任何一个文字相等,这使得下面这些比较都可以省略掉。

被检索文字列:....5......
不需要的比较: ...R.       (E和5比较时也同时发现5不等于R,于是这个比较是不必要的)
不需要的比较:  ..O..      (E和5比较时也同时发现5不等于O,于是这个比较是不必要的)
不需要的比较:   .O...     (E和5比较时也同时发现5不等于O,于是这个比较是不必要的)
不需要的比较:    M....    (E和5比较时也同时发现5不等于M,于是这个比较是不必要的)

下面附上自己写的代码:

代码的主要部分是步进数组的思想,将可显示字符对应的ASCII码和其步进长度形成一个对应关系,然后根据这个对应关系来指出遍历时的每个位置对应的字符所对应的步进长度.

#include <iostream>
#include <time.h>
using namespace std;//定义一个长字符串,以及他的大小
char CHARS[] = "beautiful, but men seldom realized it when caught by her charmas the Tarleton twins were. In her face were too sharply blended the delicate features of her mother,a Coast aristocrat of French descent, and the heavy ones of her florid Irish father. But it was anarresting face, pointed of chin, square of jaw. Her eyes were pale green without a touch of hazel,starred with bristly black lashes and slightly tilted at the ends. Above them, her thick black browsslanted upward, cutting a startling oblique line in her Seated with Stuart and Brent Tarleton in the cool shade of the porch of Tara, her father’splantation, that bright April afternoon of 1861, she made a pretty picture. Her new green flowered-muslin dress spread its twelve yards of billowing material over her hoops and exactly matched theflat-heeled green morocco slippers her father had recently brought her from Atlanta. The dress set off to perfection the seventeen-inch waist, the smallest in three counties, and the tightly fittingbasque showed breasts well matured for her sixteen years. But for all the modesty of her spreadingskirts, the demureness of hair netted smoothly into a chignon and the quietness of small whitehands folded in her lap, her true self was poorly concealed. The green eyes in the carefully sweetface were turbulent, willful, lusty with life, distinctly at variance with her decorous demeanor. Hermanners had been imposed upon her by her mother’s gentle admonitions and the sterner disciplineof her mammy; her eyes were her own.On either side of her, the twins lounged easily in their chairs, squinting at the sunlight throughtall mint-garnished glasses as they laughed and talked, their long legs, booted to the knee and thickwith saddle muscles, crossed negligently. Nineteen years old, six feet two inches tall, long of boneand hard of muscle, with sunburned faces and deep auburn hair, their eyes merry and arrogant,their bodies clothed in identical blue coats and mustard-colored breeches, they were as much alikeas two bolls of cotton.Outside, the late afternoon sun slanted down in the yard, throwing into gleaming brightness thedogwood trees that were solid masses of white blossoms against the background of new green. Thetwins’ horses were hitched in the driveway, big animals, red as their masters’ hair; and around thehorses’ legs quarreled the pack of lean, nervous possum hounds that accompanied Stuart and Brentwherever they went. A little aloof, as became an aristocrat, lay a black-spotted carriage dog,muzzle on paws, patiently waiting for the boys to go home to supper.Although born to the ease of plantation life, waited on hand and foot since infancy, the faces ofthe three on the porch were neither slack nor soft. They had the vigor and alertness of countrypeople who have spent all their lives in the open and troubled their heads very little with dullthings in books. Life in the north Georgia county of Clayton was still new and, according to thestandards of Augusta, Savannah and Charleston, a little crude. The more sedate and older sectionsof the South looked down their noses at the up-country Georgians, but here in north Georgia, alack of the niceties of classical education carried no shame, provided a man was smart in the thingsthat mattered. And raising good cotton, riding well, shooting straight, dancing lightly, squiring theladies with elegance and carrying one’s liquor like a gentleman were the things that mattered.It was for this precise reason that Stuart and Brent were idling on the porch of Tara this Aprilafternoon. They had just been expelled from the University of Georgia, the fourth university thathad thrown them out in two years; and their older brothers, Tom and Boyd, had come home withthem, because they refused to remain at an institution where the twins were not welcome. Stuartand Brent considered their latest expulsion a fine joke, and Scarlett, who had not willingly opened a book since leaving the Fayetteville Female Academy the year before, thought it just as amusingas they did.“I know you two don’t care about being expelled, or Tom either,” she said. “But what aboutBoyd? He’s kind of set on getting an education, and you two have pulled him out of the Universityof Virginia and Alabama and South Carolina and now Georgia. He’ll never get finished at thisrate.”She meant what she said, for she could never long endure any conversation of which she wasnot the chief subject. But she smiled when she spoke, consciously deepening her dimple andfluttering her bristly black lashes as swiftly as butterflies’ wings. The boys were enchanted, as shehad intended them to be, and they hastened to apologize for boring her. They thought none the lessof her for her lack of interest. Indeed, they thought more. War was men’s business, not ladies’, andthey took her attitude as evidence of her femininity.";
#define MAXSIZE strlen(CHARS)//定义搜索字符串,以及其长度
#define search "when"
int Ssize  = strlen(search);//步进表,,大小为128,对应着ASCII码的个数
#define stepsize 512
int stepvalue[stepsize];//制作步进表,即,所要搜索的字符串中每个元素所对应的步进的值,假如不属于里面的,则步进值为字符串的大小,
void maketable(char *keys)
{int i = 0;//先将步进表里面所有的值置为关键字长度for (;i < stepsize;i++){stepvalue[i] = Ssize;}for (i = 0;i <= Ssize-2;i++)//由于是倒序搜索,所以第一个元素的步进值为Ssize-1,最后一个元素的步进值应该为Ssize,所以循环应该循环到倒数第二个数为止.
    {/*if (Ssize-i-1 < stepvalue[keys[i]])//假如步进值小于才赋值,比如texture这个关键字, t的前进值应该取后面的3而不是取前面的7,所以我们比较下,除非新的步进值小于原先的,才会赋值{stepvalue[keys[i]] = Ssize-i-1;}*/stepvalue[keys[i]] = Ssize-i-1;//上面忘记考虑了一点,就是到后面重新赋值的肯定会比前面赋值的要小,所以不用进行比较,优化代码
    }
}//比较两个字符串是否相等,true表示相等,false表示不相等,从开始处开始前进行比较,一共有size个元素
bool Comparechars(char *char_1,char *char_2,int size)
{//bool check = true;int i = 0;while (i <= size-1){//先查看指针是否有效,否则返回错误if (char_1-i == NULL || char_2+size-1-i == NULL){return false;//及时的返回,避免后面无意义的比较}//进行比较if (*(char_1-i) != *(char_2+size-1-i)){return false;}i++;}return true;
}//在search字符串中搜索是否有字符*a
/*返回值说明:
true:存在
false:不存在
*/
bool InSearch(char *a)
{char temp[] = search;for (int i = 0;i <= Ssize-1;i++){if (*a == temp[i]){return true;}}return false;
}int main()
{int count_num = 0;//用来计算共找到多少个指定字符串clock_t start,end;//用于计时unsigned int s = Ssize-1;//遍历游标
maketable(search);//制造步进表
cout<<"第 ";//开始循环start = clock()    ;while(s <= strlen(CHARS)-1){//假如当前位置是指定字符串if (Comparechars(CHARS+s,search,Ssize)){count_num++;cout<<s - Ssize+1<<" ";}//不是指定字符串,按照步进表进行步进if (CHARS[s] > 126 || CHARS[s] < 32){s += Ssize;continue;}s += stepvalue[CHARS[s]];}//搜索完成end = clock();cout<<" 处找到指定字符串"<<endl;cout <<"所取字符串大小为: "<<MAXSIZE<<"其中找到的字符串有"<<count_num<<"个"<<endl;cout<<"总共花费了"<<(double)(end - start) / CLK_TCK<<"秒"<<endl;return 0;
}

Boyer- Moore

下面是查找when字符串的结果,可以看到这个性能刚刚的!(因为在计算的时间函数时用到了(double)(end - start)/CLK_TCK,由于数值很小,转换为double后精度丢失,为0,可见程序运行时间很短,算法的时间复杂度相当小)

看下when的

下面我们再看看查找the字符串的结果

我们可以看到随着字符串的串长减小,出现频率增多,所花费的时间越短,原因在于,串长越长,每次步进的长度越长,遍历完整个字符串所花费的时间越短,出现的频率越多,用于检验的时间越多,遍历的时间越多,Boyer-Moore算法在长字符串的核对方面性能相当强悍!!

转载于:https://www.cnblogs.com/color-my-life/p/3263632.html

字符串核对之Boyer-Moore算法相关推荐

  1. boyer moore算法 java_Boyer-Moore算法

    坏字符规则: 后移位数 = 坏字符的位置 - 搜索词中的上一次出现位置 例如: 字符串: "HERE IS A SIMPLE EXAMPLE" 搜索串: "EXAMPLE ...

  2. Boyer–Moore BM 后缀匹配算法

    前言 这几年一直在it行业里摸爬滚打,一路走来,不少总结了一些python行业里的高频面试,看到大部分初入行的新鲜血液,还在为各样的面试题答案或收录有各种困难问题 于是乎,我自己开发了一款面试宝典,希 ...

  3. Boyer–Moore majority vote algorithm(摩尔投票算法)

    Boyer–Moore majority vote algorithm 摩尔投票算法 Leetcode15: https://leetcode.com/problems/majority-elemen ...

  4. C语言实现boyer moore(博伊尔-摩尔搜索)算法(附完整源码)

    boyer moore博伊尔-摩尔搜索算法 实现以以下相关接口 boyer moore博伊尔-摩尔搜索算法的完整源码(定义,实现,main函数测试) 实现以以下相关接口 int max(int a, ...

  5. 摩尔投票法(Boyer–Moore majority vote algorithm)

    参考资料 论文MJRTY A Fast Majority Vote Algorithm 算法演示网站 维基百科 算法解读 概述 摩尔投票法(Boyer–Moore majority vote algo ...

  6. 【KMP】572: Boyer–Moore–Horspool algorithm

    题目: http://acm.swust.edu.cn/#/problem/572/490 题目描述 题目内容来自:https://en.wikipedia.org/w/index.php?title ...

  7. java 类数组_Java常用类-字符串、日期类、算法及数组工具类等

    大家好,乐字节的小乐又和大家见面了.这次要给大家讲述的是Java常用类. 主要有以下知识点: Ø 字符串相关类(String .StringBuffer.StringBuilder) Ø 算法及数组工 ...

  8. python 正则表达式判断字符串是否为回文_JS使用栈判断给定字符串是否是回文算法示例...

    本文实例讲述了JS使用栈判断给定字符串是否是回文算法.分享给大家供大家参考,具体如下: /*使用栈stack类的实现*/ function stack() { this.dataStore = []; ...

  9. 字符串集合中相互包含关系算法

    字符串集合中相互包含关系算法 先介绍下使用场景,源文件是一个excel表格,要根据某一列的值生成正则表达式,为保证正则表达式的准确性,被包含的数据肯定是不能直接生成正则表达式的,例如,产科和妇产科,如 ...

  10. Python:实现字符串Z 函数或 Z 算法(附完整源码)

    Python:实现字符串Z 函数或 Z 算法 def z_function(input_str: str) -> list[int]:z_result = [0 for i in range(l ...

最新文章

  1. mysql--多实例启动方法
  2. DeepMind助力Waymo!提升自动驾驶AI准确率,还能加快模型训练
  3. 如何自己养卡,快速提升信用卡额度?
  4. SpringBoot-异常处理
  5. mipi 调试经验【转】
  6. bzoj3195 [Jxoi2012]奇怪的道路
  7. java设计模式-工厂系列
  8. Unix 目录结构是怎么由来的?
  9. 系统整理 精讲 swift 泛型
  10. 如何将文件快速拷入自己的谷歌云盘
  11. 解决hash冲突的三种方法
  12. 2月书讯 | 这本书让无数读者盼了千万遍
  13. QT 对话框不在任务栏显示
  14. Flutter中的widget
  15. linux ikev1切换到ikev2,IKEv2与IKEv1的差异.doc
  16. 简述c++语言的特点(优点)
  17. matlab识别中国象棋棋盘,一种基于图像处理的中国象棋识别系统及方法与流程
  18. GEE学习笔记 六十七:【GEE之Python版教程一】GEE学习背景介绍
  19. 魔兽世界怀旧服务器无限刷怪点,魔兽世界怀旧服:50级物理刷怪升级攻略!无干扰每小时4万经验!...
  20. 详解 Spark RDD 的转换操作与行动操作

热门文章

  1. cesium类文档查找
  2. MySQL查询拼音按首字母查询名称
  3. [毕业设计]大数据电影数据分析可视化
  4. Android文件类型转换(已适用于Android11)
  5. 数值分析:矩阵求逆-奇异性、条件数
  6. Python大牛之路所需技术和工具
  7. 可口可乐造型设计_可口可乐弧形瓶艺术设计大赛最佳作品赏析
  8. 视频直播技术分享:一文读懂主流视频直播系统的推拉流架构、传输协议等
  9. zdm各命令的功能和作用_zdm软件命令大全.xls
  10. 数控服务器管理系统,分布式数控系统(DNC)服务器的设计与实现