数据结构与算法JavaScript (四) :串(BF)

news/2024/11/14 12:04:28/

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

串的逻辑结构和线性表很相似的,不同的是串针对是是字符集,所以在操作上与线性表还是有很大区别的。线性表更关注的是单个元素的操作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查找方法的实现了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 = 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;
}

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

这里值得注意:

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

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

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

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

优化算法(一)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function BF_Optimize(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;
}

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

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

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

优化算法(二)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
         }
     }
}

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

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

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!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


http://www.ppmy.cn/news/674382.html

相关文章

ROS域名解析问题记录(蓝桥云课ros.asc)

需要用到的网站&#xff1a; ipaddress.com 需要自己查阅&#xff1a; 然后可以尝试ping一下如下&#xff1a; IPv4和IPv6都支持的 Microsoft Windows [版本 10.0.22572.201] (c) Microsoft Corporation。保留所有权利。C:\Users\zhangrelay>ping 185.199.108.133正在 Pin…

Git之解决error.GitError: manifests rev-list ^HEAD(三十一)

1.报错&#xff1a; Traceback (most recent call last): File "aosp/.repo/repo/main.py", line 651, in <module> _Main(sys.argv[1:]) File "aosp/.repo/repo/main.py", line 627, in _Main result run() File "aosp/.repo/…

FFmpeg入门详解之109:m3u8加密--HLS 架构简介及播放加密的HLS

HLS 概述 HLS 全称是 HTTP Live Streaming, 是一个由 Apple 公司实现的基于 HTTP 的媒体流传输协议. 他跟 DASH 协议的原理非常类似. 通过将整条流切割成一个小的可以通过 HTTP 下载的媒体文件, 然后提供一个配套的媒体列表文件, 提供给客户端, 让客户端顺序地拉取这些媒体文件…

C++Yolov4目标检测实战

Introduction 今年2月份&#xff0c;Yolo之父Joseph Redmon由于Yolo被用于军事和隐私窥探退出CV界表示抗议&#xff0c;就当我们以为Yolo系列就此终结的时候&#xff0c;4月24日&#xff0c;Yolov4横空出世&#xff0c;新的接棒者出现&#xff0c;而一作正是赫赫有名的AB大神。…

YOLO系列(v1~v3)的学习及YOLO-Fastest在海思平台的部署(下)

YOLO系列&#xff08;v1~v3&#xff09;的学习及YOLO-Fastest在海思平台的部署&#xff08;上&#xff09; YOLO系列&#xff08;v1~v3&#xff09;的学习及YOLO-Fastest在海思平台的部署&#xff08;中&#xff09; YOLO系列&#xff08;v1~v3&#xff09;的学习及YOLO-Fastes…

YOLOV4 论文原理 模型分析 win10 vs2015 cuda9 opencv3.3 代码测试 网盘权重yolov4.conv.137 yolov4.weight下载

提出目标&#xff1a;相比低计算量(BFLOP)&#xff0c;更着重于优化并行计算&#xff0c;在production system中实现快速计算。 YOLOv4的作者阵容里并没有Joe Redmon, 一作为俄罗斯 Alexey Bochkovskiy &#xff0c;是 YOLO 的 windows 版本github的作者。 并得到YOLO官方githu…

DarkNet框架下YOLO-v4算法推理过程的特征图可视化

说明 本篇文章针对AB版DarkNet源代码进行了修改&#xff0c;添加了一些函数&#xff0c;进行全通道特征图可视化。主要针对YOLO-v4推理过程中&#xff0c;对中间计算结果的特征图数据进行转换&#xff0c;将转换结果用表示成图片进行保存。 DarkNet源代码修改 在network_ker…

Ubuntu18.04下安装NVIDIA显卡驱动、docker、nvidia-docker;容器中编译安装opencv-4.4.0与darknet-yolov4并完成测试;容器封装镜像转移。2022

记录一下第一次在CSDN发博客&#xff0c;欢迎大家光临~ 文章目录 前言一、宿主机配置1.安装Ubunntu18.04 64位系统2.为宿主机系统更换国内软件源Ubuntu 官方源服务器在欧洲&#xff0c;国内访问很慢。所以这里有必要将软件源更换为国内的源,2.1-复制以下内容2.2-备份文件2.3-更…