-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoyerMoore.cpp
More file actions
74 lines (65 loc) · 1.56 KB
/
Copy pathBoyerMoore.cpp
File metadata and controls
74 lines (65 loc) · 1.56 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
/**
* Author: Kevin Li
* Lang: C++
* Description: Boyer Moore string search algorithm
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
#define ALPH 256
#define pb push_back
struct boyermoore {
int alph;
string s;
int *bad;
vector<int> indices;
boyermoore () {}
boyermoore (string _s, int _alph) : s(_s), alph(_alph) {
bad = new int[alph];
}
void process() {
for (int i = 0; i < alph; i++) {
bad[i] = -1;
}
for (int i = 0; i < s.length(); i++) {
bad[(int)s[i]] = i;
}
}
void search(string S) {
while (!indices.empty()) indices.pop_back();
int shift = 0;
while (shift <= (S.length()-s.length())) {
int i = s.length()-1;
while (i >= 0 && s[i] == S[shift+i]) {
i--;
}
if (i < 0) {
indices.pb(shift);
if (shift + s.length() < S.length()) {
shift += s.length() - bad[S[shift+s.length()]];
} else {
shift += 1;
}
} else {
shift += max(1,i-bad[S[shift+i]]);
}
}
}
void print() {
for (int i = 0; i < indices.size(); i++) {
cout << indices[i] << " ";
}
cout << endl;
}
};
string S,s;
int main() {
cin >> s >> S;
boyermoore BM = boyermoore(s,ALPH);
BM.process();
BM.search(S);
BM.print();
}