Skip to content

Commit 8512051

Browse files
committed
fix bug of boyer-moore
1 parent 0724b98 commit 8512051

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

Classical algorithm problem/Boyer-Moore.html

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
</head>
77
<body>
88
<script>
9-
const MAX_CHAR_SIZE = 256;
10-
11-
function BoyerMoore(text, pattern,) {
9+
function BoyerMoore(text, pattern) {
1210
let tLen = text.length,
1311
pLen = pattern.length,
14-
lastArr = Array(MAX_CHAR_SIZE),
12+
lastArr = [],
1513
matchArr = Array(pLen),
1614
suffixArr = Array(pLen);
1715

@@ -31,7 +29,7 @@
3129
j--;
3230
i--;
3331
} else {
34-
lastIndex = lastArr[text.charAt(i)] ? lastArr[text.charAt(i)] : -1;
32+
lastIndex = lastArr[text.charAt(i)] >= 0 ? lastArr[text.charAt(i)] : -1;
3533
i += pLen - 1 - j + Math.max(j - lastIndex, matchArr[j]);
3634
j = pLen - 1
3735
}
@@ -62,21 +60,18 @@
6260
}
6361
}
6462

65-
if (suffixArr[0] < pattern.length) {
66-
for (let j = suffixArr[0] - 1; j >= 0; j--) {
67-
if (suffixArr[0] < matchArr[j]) {
68-
matchArr[j] = suffixArr[0]
69-
}
70-
}
7163

72-
let j = suffixArr[0];
73-
for (let k = suffixArr[j]; k < pattern.length; k = suffixArr[k]) {
74-
while (j < k) {
75-
if (matchArr[j] > k) {
76-
matchArr[j] = k
64+
let k = 0;
65+
while (k < pLen - 1) {
66+
if (suffixArr[k] < pattern.length) {
67+
for (let j = suffixArr[k] - 1; j >= 0; j--) {
68+
if (suffixArr[k] < matchArr[j]) {
69+
matchArr[j] = suffixArr[k]
7770
}
78-
j++
7971
}
72+
break;
73+
} else {
74+
k++
8075
}
8176
}
8277
};
@@ -98,8 +93,7 @@
9893
match()
9994
}
10095

101-
102-
BoyerMoore("HERE IS A SIMPLE LEAMPLE", "LEAMPLE")
96+
BoyerMoore("here is a rxleple example", "example")
10397
</script>
10498
</body>
10599
</html>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy