forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoyer_Moore.cs
More file actions
110 lines (82 loc) · 2.42 KB
/
Boyer_Moore.cs
File metadata and controls
110 lines (82 loc) · 2.42 KB
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
// C# Program for Boyer Moore String Matching Algorithm
using System;
public class Algorithm
{
static int CHARACTERS = 256;
// Getting maximum of two integers
static int max (int a, int b) { return (a > b)? a: b; }
// Bad Character Pre-Processing Function
static void badChar( char []str, int size,int []badCharacter)
{
int i;
// Initializing all occurences to -1
for (i = 0; i < CHARACTERS; i++)
badCharacter[i] = -1;
// Filling the Actual Value
for (i = 0; i < size; i++)
badCharacter[(int) str[i]] = i;
}
// Pattern Searching Function
static void search( char []txt, char []pat)
{
int m = pat.Length;
int n = txt.Length;
int []Character = new int[CHARACTERS];
badChar(pat, m, Character);
/*
s is used to keep track of
pattern shifting with respect to text
*/
int s = 0;
while(s <= (n - m))
{
int j = m - 1;
while(j >= 0 && pat[j] == txt[s+j])
j--;
/*
If the pattern is present at current
shift, then index j will become -1 after
the above loop
*/
if (j < 0)
{
Console.WriteLine("Pattern occurs at index: " + s);
s += (s+m < n)? m-Character[txt[s+m]] : 1;
}
else
s += max(1, j - Character[txt[s+j]]);
}
}
public static void Main()
{
Console.WriteLine("Enter The String Value: ");
String valueEntered = Console.ReadLine();
Console.WriteLine("Enter The Pattern To Search: ");
String pattern = Console.ReadLine();
Console.WriteLine();
char []txt = valueEntered.ToCharArray();
char []pat = pattern.ToCharArray();
search(txt, pat);
}
}
/**
Enter The String Value:
ABAAABCD
Enter The Pattern To Search:
ABC
Pattern occurs at index: 4
--------------------------------------------------
Enter The String Value:
AABAACAADAABAABA
Enter The Pattern To Search:
AABA
Pattern occurs at index: 0
Pattern occurs at index: 9
Pattern occurs at index: 12
--------------------------------------------------
Enter The String Value:
THIS IS A TEST TEXT
Enter The Pattern To Search:
TEST
Pattern occurs at index: 10
*/