File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
cs-algorithms/Strings/Search Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
2
+
3
+ namespace Algorithms . Strings . Search
4
+ {
5
+ public class BoyerMoore
6
+ {
7
+ private int [ ] right ;
8
+ private const int R = 256 ;
9
+ private int M ;
10
+ private string pat ;
11
+ public BoyerMoore ( string pat )
12
+ {
13
+ this . pat = pat ;
14
+ right = new int [ R ] ;
15
+ for ( int i = 0 ; i < pat . Length ; ++ i )
16
+ {
17
+ right [ pat [ i ] ] = i ;
18
+ }
19
+ M = pat . Length ;
20
+ }
21
+
22
+ public int Search ( string text )
23
+ {
24
+ int skip = 1 ;
25
+ for ( int i = 0 ; i < text . Length - M ; i += skip )
26
+ {
27
+ var j ;
28
+ for ( j = M - 1 ; j >= 0 ; j -- )
29
+ {
30
+ if ( text [ i + j ] != pat [ j ] )
31
+ {
32
+ skip = Math . Max ( 1 , j - right [ text [ i + j ] ] ) ;
33
+ break ;
34
+ }
35
+ }
36
+ if ( j == - 1 ) return i ;
37
+ }
38
+ return - 1 ;
39
+ }
40
+
41
+ }
42
+ }
You can’t perform that action at this time.
0 commit comments