-
Notifications
You must be signed in to change notification settings - Fork 0
/
pBq4.cpp
51 lines (44 loc) · 953 Bytes
/
pBq4.cpp
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
#include <stdio.h>
#include <string.h>
#include "math.h"
#include <bits/stdc++.h>
using namespace std;
#define characters 256
void badCharacters(string pattern, int m, int badC[characters])
{
for (int c = 0; c < characters; c++)
badC[c] = -1;
for (int c = 0; c < m; c++)
badC[(int) pattern[c]] = c;
}
void moore( string text, string pattern)
{
int m = pattern.size();
int n = text.size();
int badC[characters];
badCharacters(pattern, m, badC);
int c=0;
while (1)
{
if (c>(n-m))
break;
int d = m-1;
while (d>=0 && pattern[d]==text[c+d])
d--;
if (d<0)
{
cout<<"Pattern matches at shift: "<<c<<endl;
c = c + (c + m < n)? m-badC[text[c + m]] : 1;
}
else
{ c = c + max(1, d - badC[text[c + d]]);
}
}
}
int main()
{
string txt= "ABAAABCD";
string pat = "ABC";
moore(txt, pat);
return 0;
}