-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubstrSearch.cc
155 lines (148 loc) · 3.53 KB
/
SubstrSearch.cc
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <print>
#include <string_view>
#include <vector>
int naiveMethod(std::string_view pat, std::string_view txt) {
int M = pat.size();
int N = txt.size();
// i <= ((N - 1) - M) + 1
for (int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break;
if (j == M)
return i;
}
return N;
}
int bruteForce(std::string_view pat, std::string_view txt) {
int i, N = txt.size();
int j, M = pat.size();
for (i = 0, j = 0; i < N && j < M; ++i) {
if (txt[i] == pat[j])
j++;
else {
// i = (i - (j + 1)) + 1
i = i - j;
j = 0;
}
}
return j == M ? i - M : N;
}
struct KMP {
static std::vector<int> next(std::string_view pat) {
int m = pat.size(), j = 0;
std::vector<int> N(m);
int t = N[0] = -1;
while (j < m - 1)
if (t < 0 || pat[j] == pat[t])
N[++j] = ++t;
else
t = N[t];
return N;
}
static int search(std::string_view pat, std::string_view txt) {
int n = txt.size(), i = 0;
int m = pat.size(), j = 0;
std::vector<int> N = next(pat);
while (j < m && i < n)
if (j < 0 || txt[i] == pat[j])
++i, ++j;
else
j = N[j];
return i - j;
}
};
struct ImprovedKMP {
static std::vector<int> next(std::string_view pat) {
int m = pat.size(), j = 0;
std::vector<int> N(m);
int t = N[0] = -1;
while (j < m - 1)
if (t < 0 || pat[j] == pat[t]) {
++j, ++t;
if (pat[j] != pat[t])
N[j] = t;
else
N[j] = N[t];
} else
t = N[t];
return N;
}
static int search(std::string_view pat, std::string_view txt) {
int n = txt.size(), i = 0;
int m = pat.size(), j = 0;
std::vector<int> N = next(pat);
while (j < m && i < n)
if (j < 0 || txt[i] == pat[j])
++i, ++j;
else
j = N[j];
return i - j;
}
};
struct BM {
static std::vector<int> right(std::string_view pat) {
std::vector<int> right(256);
for (int c = 0; c < 256; c++)
right[c] = -1;
for (int j = 0; j < pat.size(); j++)
right[pat[j]] = j;
return right;
}
static int search(std::string_view pat, std::string_view txt) {
int N = txt.length();
int M = pat.length();
std::vector<int> A(right(pat));
int skip;
for (int i = 0; i <= N - M; i += skip) {
skip = 0;
for (int j = M - 1; j >= 0; j--) {
if (pat.at(j) != txt.at(i + j)) {
skip = std::max(1, j - A[txt.at(i + j)]);
break;
}
}
if (skip == 0)
return i;
}
return N;
}
};
int main() {
char txt[]{"BAAABAABBB"};
char pat[]{"AAABAAB"};
std::print("BF");
int a{naiveMethod(pat, txt)};
std::print("\n{}\t\n", txt);
for (int i = 0; i < a; ++i)
std::print(" ");
std::print("{}\t\n\n", pat);
// KMP
std::print("KMP\t");
std::vector<int> vec1{KMP::next(pat)};
for (const auto &e : vec1)
std::print("{}\t", e);
int b{KMP::search(pat, txt)};
std::print("\n{}\t\n", txt);
for (int i = 0; i < b; ++i)
std::print(" ");
std::print("{}\t\n\n", pat);
// ImprovedKMP
std::print("IKMP\t");
std::vector<int> vec2{ImprovedKMP::next(pat)};
for (const auto &e : vec2)
std::print("{}\t", e);
int c{ImprovedKMP::search(pat, txt)};
std::print("\n{}\t\n", txt);
for (int i = 0; i < c; ++i)
std::print(" ");
std::print("{}\t\n\n", pat);
// BM
std::print("BM\t");
int d{BM::search(pat, txt)};
std::print("\n{}\t\n", txt);
for (int i = 0; i < d; ++i)
std::print(" ");
std::print("{}\t\n", pat);
}