forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoyer_Moore.c
63 lines (51 loc) · 1.17 KB
/
Boyer_Moore.c
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
#include <stdio.h>
#define MAX 256
int findmax(int a, int b)
{
return (a > b) ? a : b;
}
void searchwrongchar(char *str, int size, int extra[MAX])
{
for (int i = 0; i < MAX; ++i)
extra[i] = -1;
for (int i = 0; i < size; ++i)
extra[(int)str[i]] = i;
}
void search(char *text, char *pattern)
{
int patLen = strlen(pattern);
int txtLen = strlen(text);
int extra[MAX];
searchwrongchar(pattern, patLen, extra);
int s = 0;
while (s <= (txtLen - patLen))
{
int j = patLen - 1;
while (j >= 0 && pattern[j] == text[s + j])
j--;
if (j < 0)
{
printf("\n\nPattern Matches at shift of = %d.", s);
s += (s + patLen < txtLen) ? patLen - extra[text[s + patLen]] : 1;
}
else
s += findmax(1, j - extra[text[s + j]]);
}
}
int main()
{
char text[MAX], pattern[MAX];
printf("Enter the Text: ");
scanf("%s", &text);
printf("\nEnter the Pattern to Match: ");
scanf("%s", &pattern);
search(text, pattern);
return 0;
}
/*
INPUT:
Enter the Text: ABAAABCD
Enter the Pattern to Match: ABC
OUTPUT:
Pattern match at shift = 4
*/