-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathBM_algo.cpp
More file actions
60 lines (40 loc) · 1005 Bytes
/
BM_algo.cpp
File metadata and controls
60 lines (40 loc) · 1005 Bytes
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
#include <bits/stdc++.h>
using namespace std;
# define NO_OF_CHARS 256
void badCharHeuristic( string str, int size,
int badchar[NO_OF_CHARS])
{
int i;
for (i = 0; i < NO_OF_CHARS; i++)
badchar[i] = -1;
for (i = 0; i < size; i++)
badchar[(int) str[i]] = i;
}
void search( string txt, string pat)
{
int q = pat.size();
int p = txt.size();
int badchar[NO_OF_CHARS];
badCharHeuristic(pat, q, badchar);
int t = 0;
while(t <= (p - q))
{
int j = q - 1;
while(j >= 0 && pat[j] == txt[t + j])
j--;
if (j < 0)
{
cout << "pattern occurs at shift = " << t << endl;
t += (t + q < p)? q-badchar[txt[t + q]] : 1;
}
else
t += max(1, j - badchar[txt[t + j]]);
}
}
int main()
{
string txt= "ABCDAABB";
string pat = "ABC";
search(txt, pat);
return 0;
}