串是由零个或多个字符组成的有限序列,又叫做字符串

串的逻辑结构和线性表很相似的,不同的是串针对是是字符集,所以在操作上与线性表还是有很大区别的。线性表更关注的是单个元素的操作CURD,串则是关注查找子串的位置,替换等操作。

当然不同的高级语言对串的基本操作都有不同的定义方法,但是总的来说操作的本质都是相似的。比如javascrript查找就是indexOf, 去空白就是trim,转化大小写toLowerCase/toUpperCase等等

这里主要讨论下字符串模式匹配的几种经典的算法:BF、BM、KMP


BF(Brute Force)算法

Brute-Force算法的基本思想:

从目标串s 的第一个字符起和模式串t的第一个字符进行比较,若相等,则继续逐个比较后续字符,否则从串s 的第二个字符起再重新和串t进行比较。

依此类推,直至串t 中的每个字符依次和串s的一个连续的字符序列相等,则称模式匹配成功,此时串t的第一个字符在串s 中的位置就是t 在s中的位置,否则模式匹配不成功

可见BF算法是一种暴力算法,又称为朴素匹配算法或蛮力算法。

 


主串 BBC ABB ABCF

子串 ABC

在主串中找出子串的位置,对应了其实就是javascript的indexOf查找方法的实现了

var sourceStr = "BBC ABB ABCF";
var searchStr = "ABC";

function BF_Ordinary(sourceStr, searchStr) {
var sourceLength = sourceStr.length;
var searchLength = searchStr.length;
var padding = sourceLength - searchLength; //循环的次数
//BBC ABB ABCF =>ABC => 搜索9次
for (var i = ; i <= padding; i++) {
//如果满足了第一个charAt是相等的
//开始子循环检测
//其中sourceStr的取值是需要叠加i的值
if (sourceStr.charAt(i) == searchStr.charAt()) {
//匹配成功的数据
var complete = searchLength;
for (var j = ; j < searchLength; j++) {
if (sourceStr.charAt(i + j) == searchStr.charAt(j)) {
--complete
if (!complete) {
return i;
}
}
}
}
}
return -;
}

BF算法就是简单粗暴,直接把BBC ABB ABCF母串的每一个字符的下表取出来与模式串的第一个字符匹配,如果相等就进去字串的再次匹配

这里值得注意:

1:最外围循环的次数sourceLength - searchLength,因为我们匹配的母串至少要大于等于子串

2:在子串的继续匹配中,母串的起点是需要叠加的(i+j)

3:通过一个条件判断是否完全匹配complete,BBC ABB ABCF中,我们在ABB的时候就需要跳过去

上面是最简单的一个算法了,代码上还有更优的处理,比如在自串的匹配上可以采取取反的算法

优化算法(一)

function BF_Optimize(sourceStr, searchStr) {
var mainLength = sourceStr.length;
var searchLength = searchStr.length;
var padding = mainLength - searchLength;
for (var offset = ; offset <= padding; offset++) {
var match = true;
for (var i = ; i < searchLength; i++) {
//取反,如果只要不相等
if (searchStr.charAt(i) !== sourceStr.charAt(offset + i)) {
match = false;
break;
}
}
if (match) return offset;
}
return -;
}

我们不需要判断为真的情况,我们只要判断为假的情况就可以了,当子匹配结束后match没有被修改过的话,则说明此匹配是完全匹配

以上2种方法我们都用到了子循环,我们能否改成一个循环体呢?

其实我们可以看到规律,主串每次都只会递增+1,子串每次匹配也是从头开始匹配,所以我们可以改成一个while,控制下标指针就可以了

优化算法(二)

function BF_Optimize_2(sourceStr, searchStr) {
var i = ,
j = ; while (i < sourceStr.length) {
// 两字母相等则继续
if (sourceStr.charAt(i) == searchStr.charAt(j)) {
i++;
j++;
} else { // 两字母不等则角标后退重新开始匹配
i = i - j + ; // i 回退到上次匹配首位的下一位
j = ; // j 回退到子串的首位
} if (j == searchStr.length) {
return i - j;
} }
}

i就是主串的下标定位,j就是子串的下标定位

当主串子串相等的时候,就进入了子串的循环模式,当子循环的次数j满足子串长度时,就验证是完全匹配

当主串子串不相等的时候,就需要把主串的下标往后移一位,当然i的时候,因为可能经过子串的处理,所以需要i-j+1, 然后复位子串

具体我们可以看看代码比较

基于BF算法的四种结构,for/while/递归

<!doctype html>由于电脑性能的不断提高,测试的数据量的大小,可能会导致得到的结果不太准确;<script type="text/javascript">

/////////
//暴力算法 //
//普通版
/////////
function BF_Ordinary(sourceStr, searchStr) {
var sourceLength = sourceStr.length;
var searchLength = searchStr.length;
var padding = sourceLength - searchLength; //循环的次数

//BBC ABB ABCF =>ABC => 搜索9次
for (var i = 0; i <= padding; i++) {
//如果满足了第一个charAt是相等的
//开始子循环检测
//其中sourceStr的取值是需要叠加i的值
if (sourceStr.charAt(i) == searchStr.charAt(0)) {
//匹配成功的数据
var complete = searchLength;
for (var j = 0; j < searchLength; j++) {
if (sourceStr.charAt(i + j) == searchStr.charAt(j)) {
--complete
if (!complete) {
return i;
}
}
}
}
}
return -1;
}

/////////
//暴力算法 //
//优化版
/////////
function BF_Optimize_1(sourceStr, searchStr) {
var mainLength = sourceStr.length;
var searchLength = searchStr.length;
var padding = mainLength - searchLength;
for (var offset = 0; offset <= padding; offset++) {
var match = true;
for (var i = 0; i < searchLength; i++) {
//取反,如果只要不相等
if (searchStr.charAt(i) !== sourceStr.charAt(offset + i)) {
match = false;
break;
}
}
if (match) return offset;
}
return -1;
}

////////
//优化版 //
//while
////////
function BF_Optimize_2(sourceStr, searchStr) {
var i = 0,
j = 0;

while (i < sourceStr.length) {
// 两字母相等则继续
if (sourceStr.charAt(i) == searchStr.charAt(j)) {
i++;
j++;
} else { // 两字母不等则角标后退重新开始匹配
i = i - j + 1; // i 回退到上次匹配首位的下一位
j = 0; // j 回退到子串的首位
}

if (j == searchStr.length) {
return i - j;
}

}
}

/////////
//暴力算法
//递归版本
/////////
function BF_Recursive(sourceStr, searchStr, offset) {
var mainLength = sourceStr.length;
var searchLength = searchStr.length;
if (searchLength > mainLength - offset) {
return -1;
}
offset = offset || 0;
for (var i = 0; searchLength > i; i++) {
if (searchStr.charAt(i) !== sourceStr.charAt(offset + i)) {
return BF_Recursive(sourceStr, searchStr, offset + 1)
}
}
return offset;
}

var sourceStr = "There are some times wThere are some times when clicking “like” on a friend’s Facebook status doesn’t feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg’s radar for a while, he said. In 2010, he told ABC News’ Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend’s Facebook status doesn’t feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg’s radar for a while, he said. In 2010, he told ABC News’ Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend’s Facebook status doesn’t feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg’s radar for a while, he said. In 2010, he told ABC News’ Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend’s Facebook status doesn’t feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg’s radar for a while, he said. In 2010, he told ABC News’ Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend’s Facebook status doesn’t feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg’s radar for a while, he said. In 2010, he told ABC News’ Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend’s Facebook status doesn’t feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg’s radar for a while, he said. In 2010, he told ABC News’ Diane Sawyer that that Facebook would “definitely thinkhen clicking “like” on a friend’s Facebook status doesn’t feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg’s radar for a while, he said. In 2010, he told ABC News’ Diane Sawyer There are some times when clicking “like” on a friend’s Facebook status doesn’t feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg’s radar for a while, he said. In 2010, he told ABC News’ Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend’s Facebook status doesn’t feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg’s radar for a while, he said. In 2010, he told ABC News’ Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend’s Facebook status doesn’t feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg’s radar for a while, he said. In 2010, he told ABC News’ Diane Sawyer that that Facebook would “definitely thinkThere are some times when clicking “like” on a friend’s Facebook status doesn’t feel appropriate. A bad day. A loved one lost. A break up. It only seems natural that a “dislike” button could solve the conundrum of wanting to empathize but not seem inappropriate by clicking “like.” Mark Zuckerberg Puts the Rest of Us to Shame by Speaking Fluent Chinese. Mark Zuckerberg: Facebook Founder and Animal Butcher. Mark Zuckerberg and That Shirt. The idea has been on Mark Zuckerberg’s radar for a while, he said. In 2010, he told ABC News’ Diane Sawyer that that Facebook would “definitely think that that Facebook would “definitely think about” adding a dislike button. “People definitely seem to want it,” Zuckerberg said. Four years later — Zuckerberg says Facebook is still “thinking about” adding the oft-requested sdfafd button, Zuckerberg says Facebook is still “thinking about” adding the oft-requested button. At a town hall meeting on Thursday, the CEO revealed he has some reservations about the feature. “There are two things that it can mean,” Zuckerberg said of the potential button, which could be used in a mean spirited way or to express empathy. Finding how to limit it to the latter is the challenge. Zuckerberg said he doesn’t want the button to turn into a “voting mechanism” or something that isn’t “socially valuable.” “Often people will tell us they don’t feel comfortable pressing ‘like,'” Zuckerberg said. “What’s the right way to make it so people can easier express a wide range of emotions?” One suggestion percolating online: Aaron Roll out the feature under a different name. However, an “empathy button” just may not have the same ring to it as “dislike.”";
var searchStr = "adding the oft-requested sdf";

function show(bf_name,fn) {
var myDate = +new Date()
var r = fn();
var div = document.createElement('div')
div.innerHTML = bf_name +'算法,搜索位置:' + r + ",耗时" + (+new Date() - myDate) + "ms";
document.body.appendChild(div);
}

show('BF_Ordinary',function() {
return BF_Ordinary(sourceStr, searchStr)
})

show('BF_Optimize_1',function() {
return BF_Optimize_1(sourceStr, searchStr)
})

show('BF_Optimize_2',function() {
return BF_Optimize_2(sourceStr, searchStr)
})

show('BF_Recursive',function() {
return BF_Recursive(sourceStr, searchStr)
})

</script>

BF也是经典的前缀匹配算法,前缀还包括KMP,我们可见这种算法最大缺点就是字符匹配失败指针就要回溯,所以性能很低,之后会写一下KMP与BM算法针对BF的的升级

git代码下载:https://github.com/JsAaron/data_structure


数据结构与算法JavaScript (四) 串(BF)的更多相关文章

  1. 数据结构与算法JavaScript (五) 串(经典KMP算法)

    KMP算法和BM算法 KMP是前缀匹配和BM后缀匹配的经典算法,看得出来前缀匹配和后缀匹配的区别就仅仅在于比较的顺序不同 前缀匹配是指:模式串和母串的比较从左到右,模式串的移动也是从 左到右 后缀匹配 ...

  2. 翻阅《数据结构与算法javascript描述》--数组篇

    导读: 这篇文章比较长,介绍了数组常见的操作方法以及一些注意事项,最后还有几道经典的练习题(面试题). 数组的定义: JavaScript 中的数组是一种特殊的对象,用来表示偏移量的索引是该对象的属性 ...

  3. 数据结构与算法javascript描述

    <数据结构与算法javascript描述>--数组篇 导读: 这篇文章比较长,介绍了数组常见的操作方法以及一些注意事项,最后还有几道经典的练习题(面试题). 数组的定义: JavaScri ...

  4. 数据结构与算法JavaScript (一) 栈

    序 数据结构与算法JavaScript这本书算是讲解得比较浅显的,优点就是用javascript语言把常用的数据结构给描述了下,书中很多例子来源于常见的一些面试题目,算是与时俱进,业余看了下就顺便记录 ...

  5. 《数据结构与算法JavaScript描述》

    <数据结构与算法JavaScript描述> 基本信息 作者: (美)Michael McMillan 译者: 王群锋 杜欢 丛书名: 图灵程序设计丛书 出版社:人民邮电出版社 ISBN:9 ...

  6. 列表的实现-----数据结构与算法JavaScript描述 第三章

    实现一个列表 script var booklist = new List(); booklist.append('jsbook'); booklist.append('cssbook'); book ...

  7. 《数据结构与算法JavaScript描述》中的一处错误

    最近在看<数据结构与算法JavaScript描述>这本书,看到选择排序这部分时,发现一个比较大的错误. 原书的选择排序算法是这样的: function selectionSort() { ...

  8. 数据结构与算法 Javascript描述

    数据结构与算法系列主要记录<数据结构与算法 Javascript描述>学习心得

  9. JavaScript 版数据结构与算法(四)集合

    今天,我们要讲的是数据结构与算法中的集合. 集合简介 什么是集合?与栈.队列.链表这些顺序数据结构不同,集合是一种无序且唯一的数据结构.集合有什么用?在 Python 中,我经常使用集合来给数组去重: ...

随机推荐

  1. [转] js对象浅拷贝和深拷贝详解

    本文为大家分享了JavaScript对象的浅拷贝和深拷贝代码,供大家参考,具体内容如下 1.浅拷贝 拷贝就是把父对像的属性,全部拷贝给子对象. 下面这个函数,就是在做拷贝: var Chinese = ...

  2. BestCoder Round #80 1002

    HDU 5666 Segment 题意:给你条斜率为-1,常数项为q(q为质数)的直线,连接原点与直线上整数格点,问你在有多少个格点在形成的无数个三角形内,而不在线段上,结果对P取模. 思路:best ...

  3. printf的特殊用法

    printf的特殊用法:对于m.n的格式可以用如下方法表示 前边的 *   定义的是总的宽度,后边的 * 定义的是输出的个数.分别对应外面的参数m和n .这种方法的好处是可以在语句之外对参数m和n赋值 ...

  4. 通读SDWebImage③--gif和webP的支持、不同格式图片的处理、方向处理

    本文目录 NSData+ImageContentType: 根据NSData获取MIME UIImage+GIF UIImage+WebP UIImage+MultiFormat:根据NSData相应 ...

  5. 破除Odoo 菜单栏提示 99+

    菜单栏提示利用的menu对象的needaction_counter属性,默认只能显示99个 多于99则显示99+ 如果希望显示完整的数字,则需要改动如下两个地方. 一.menu菜单的显示限制: 将第一 ...

  6. MySQL字符集

    字符集的选择 1.如果数据库只需要支持中文,数据量很大,性能要求也很高,应该选择双字节定长编码的中文字符集(如GBK).因为相对于UTF-8而言,GBK"较小",每个汉字只占2个字 ...

  7. css自适应宽度高级写法:一行多列~~~某些列的宽度是固定值

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxgAAACdCAIAAAC4iknFAAAgAElEQVR4nO2d3VsTV7uH51/wqhKotn

  8. wamp2.5 局域网无法访问问题

    1.打开http.conf文件,在对应处修改为如下内容(通常经过步骤一之后就能访问了,若不行则再执行后面步骤) <Directory /> Options FollowSymLinks A ...

  9. final 评论ii

    按照演讲顺序 1.约跑app         约跑app,从界面的单调,到最后的final发布,实现界面的友好性,有了很大的提高.约跑app,如果在约定地点可以显示出,所在位置,以及约定地址.就可以达 ...

  10. ubuntu1404安装搜狗输入法后出现黑框的问题

    1.安装xcompmgr   sudo apt-get install xcompmgr 2.设置xcompmgr自动启动 mkdir-/.config/autostart vim xcompmgr. ...