File tree Expand file tree Collapse file tree 1 file changed +13
-19
lines changed
Classical algorithm problem Expand file tree Collapse file tree 1 file changed +13
-19
lines changed Original file line number Diff line number Diff line change 6
6
</ head >
7
7
< body >
8
8
< script >
9
- const MAX_CHAR_SIZE = 256 ;
10
-
11
- function BoyerMoore ( text , pattern , ) {
9
+ function BoyerMoore ( text , pattern ) {
12
10
let tLen = text . length ,
13
11
pLen = pattern . length ,
14
- lastArr = Array ( MAX_CHAR_SIZE ) ,
12
+ lastArr = [ ] ,
15
13
matchArr = Array ( pLen ) ,
16
14
suffixArr = Array ( pLen ) ;
17
15
31
29
j -- ;
32
30
i -- ;
33
31
} else {
34
- lastIndex = lastArr [ text . charAt ( i ) ] ? lastArr [ text . charAt ( i ) ] : - 1 ;
32
+ lastIndex = lastArr [ text . charAt ( i ) ] >= 0 ? lastArr [ text . charAt ( i ) ] : - 1 ;
35
33
i += pLen - 1 - j + Math . max ( j - lastIndex , matchArr [ j ] ) ;
36
34
j = pLen - 1
37
35
}
62
60
}
63
61
}
64
62
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
- }
71
63
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 ]
77
70
}
78
- j ++
79
71
}
72
+ break ;
73
+ } else {
74
+ k ++
80
75
}
81
76
}
82
77
} ;
98
93
match ( )
99
94
}
100
95
101
-
102
- BoyerMoore ( "HERE IS A SIMPLE LEAMPLE" , "LEAMPLE" )
96
+ BoyerMoore ( "here is a rxleple example" , "example" )
103
97
</ script >
104
98
</ body >
105
99
</ html >
You can’t perform that action at this time.
0 commit comments