-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathshortest-matching-substring.cpp
124 lines (117 loc) · 4.11 KB
/
shortest-matching-substring.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
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
// Time: O(n + m)
// Space: O(n + m)
// kmp, two pointers (three pointers)
class Solution {
public:
int shortestMatchingSubstring(string s, string p) {
static const int INF = numeric_limits<int>::max();
const auto& KMP = [](const auto& text, const auto& pattern) {
const auto& getPrefix = [](const auto& pattern) {
vector<int> prefix(size(pattern), -1);
int j = -1;
for (int i = 1; i < size(pattern); ++i) {
while (j != -1 && pattern[j + 1] != pattern[i]) {
j = prefix[j];
}
if (pattern[j + 1] == pattern[i]) {
++j;
}
prefix[i] = j;
}
return prefix;
};
vector<int> result;
if (empty(pattern)) {
result.resize(size(text) + 1);
iota(begin(result), end(result), 0);
return result;
}
const vector<int> prefix = getPrefix(pattern);
int j = -1;
for (int i = 0; i < size(text); ++i) {
while (j > -1 && pattern[j + 1] != text[i]) {
j = prefix[j];
}
if (pattern[j + 1] == text[i]) {
++j;
}
if (j == size(pattern) - 1) {
result.emplace_back(i - j);
j = prefix[j];
}
}
return result;
};
const int i = p.find('*');
const auto& a = p.substr(0, i);
const int j = p.find('*', i + 1);
const auto& b = p.substr(i + 1, j - (i + 1));
const auto& c = p.substr(j + 1);
const int n = size(s), la = size(a), lb = size(b), lc = size(c);
int result = INF;
const auto& idxs1 = KMP(s, b);
const auto& idxs2 = KMP(s, c);
int x = 0, y = 0;
for (const auto& i : KMP(s, a)) {
for (; x < size(idxs1) && idxs1[x] < i + la; ++x);
if (x == size(idxs1)) {
break;
}
for (; y < size(idxs2) && idxs2[y] < idxs1[x] + lb; ++y);
if (y == size(idxs2)) {
break;
}
result = min(result, (idxs2[y] + lc) - i);
}
return result != INF ? result : -1;
}
};
// Time: O(n + m)
// Space: O(n + m)
// kmp, two pointers (three pointers)
class Solution2 {
public:
int shortestMatchingSubstring(string s, string p) {
static const int INF = numeric_limits<int>::max();
const auto& getPrefix = [](const string& pattern) {
vector<int> prefix(size(pattern), -1);
int j = -1;
for (int i = 1; i < size(pattern); ++i) {
while (j != -1 && pattern[j + 1] != pattern[i]) {
j = prefix[j];
}
if (pattern[j + 1] == pattern[i]) {
++j;
}
prefix[i] = j;
}
return prefix;
};
const int i = p.find('*');
const auto& a = p.substr(0, i);
const int j = p.find('*', i + 1);
const auto& b = p.substr(i + 1, j - (i + 1));
const auto& c = p.substr(j + 1);
const int n = size(s), la = size(a), lb = size(b), lc = size(c);
const auto& prefix1 = getPrefix(a + '#' + s);
const auto& prefix2 = getPrefix(b + '#' + s);
const auto& prefix3 = getPrefix(c + '#' + s);
int result = INF;
for (int i = 0, j = 0, k = 0; i + lb + lc < n; ++i) {
for (; i < n && prefix1[la + 1 + i] + 1 != la; ++i);
if (i == n) {
break;
}
for (; j < n && !(j >= i + lb && prefix2[lb + 1 + j] + 1 == lb); ++j);
if (j == n) {
break;
}
for (; k < n && !(k >= j + lc && prefix3[lc + 1 + k] + 1 == lc); ++k);
if (k == n) {
break;
}
result = min(result, k - (i - la));
}
return result != INF ? result : -1;
}
};