-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.cpp
54 lines (49 loc) · 1.05 KB
/
main.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
52
53
#include <iostream>
#include <string>
int * getNext(std::string pattern) {
// get the length of `pattern`
int len = pattern.length();
int * next = new int[len];
int index = 0;
// the first value is default zero
next[0] = 0;
for (int i = 1; i < len;) {
if (pattern[i] == pattern[index]) {
next[i] = index + 1;
index++;
i++;
} else {
if (index != 0) {
index = next[index - 1];
} else {
next[i] = 0;
i++;
}
}
}
return next;
}
int getIndex(std::string text, std::string pattern) {
int * next = getNext(pattern);
int t = 0;
int p = 0;
while (t < text.length() && p < pattern.length()) {
if (text[t] == pattern[p]) {
t++;
p++;
} else {
if (p != 0) p = next[p - 1];
else t++;
}
}
return p == pattern.length() ? t - p : -1;
}
bool hasSubstring(std::string text, std::string pattern) {
return getIndex(text, pattern) != -1;
}
int main() {
std::string text = "abcabaaabaabcac";
std::string pattern = "abaabcac";
std::cout << hasSubstring(text, pattern) << std::endl;
return 0;
}