-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_substr.cpp
80 lines (58 loc) · 2.24 KB
/
find_substr.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <string>
#include <vector>
#include <iostream>
/*
Функция возвращает номер индекса найденного образа (pattern) в строке (str).
Образ может содержать символы '?' - который заменяет любой символ
Алгоритм Кнута-Морриса-Пратта
*/
int find_substring(const std::string& str, const std::string& pattern)
{
std::vector<int> pf (pattern.length());
pf[0] = 0;
for (int k = 0, i = 1; i < pattern.length(); i++)
{
while ((k > 0) && (pattern[i] != pattern[k]))
k = pf[k-1];
if (pattern[i] == pattern[k])
k++;
pf[i] = k;
}
for (int match_len = 0, i = 0; i < str.length(); i++)
{
bool reset = false;
while ((match_len > 0) && (pattern[match_len] != str[i])) {
if(pattern[match_len] == '?') {
if(match_len > 0 && reset) {
int kmp_value = find_substring(str.substr(i-match_len, i), pattern.substr(0, match_len));
if(kmp_value != 0)
match_len = 0;
else if(pattern[match_len - 1] == '?')
match_len++;
}
break;
}
match_len = pf[match_len-1];
reset = true;
}
if(reset && pattern[match_len] == str[i-1]){
match_len++;
}
if (pattern[match_len] == str[i] || pattern[match_len] == '?')
match_len++;
if (match_len == pattern.length())
return (i - pattern.length() + 1);
}
return -1;
}
int main() {
std::cout << find_substring("ddabcd", "??bf") << '\n'; // returns -1
std::cout << find_substring("abcd", "a?cd") << '\n'; // returns 0
std::cout << find_substring("abcd", "abcd") << '\n'; // returns 0
std::cout << find_substring("abcd", "ab") << '\n'; // returns 0
std::cout << find_substring("ab", "abcd") << '\n'; // returns -1
std::cout << find_substring("ababc", "abc") << '\n'; // returns 2
std::cout << find_substring("aabaabbaaabaabaabaabaabbaabb", "aabbaab") << '\n'; // returns 20
std::cout << find_substring("aabaabbaaabaabaabaabaabbaabb", "aa?b??b") << '\n'; // returns 20
return 0;
}