forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-all-good-strings.cpp
73 lines (70 loc) · 2.48 KB
/
find-all-good-strings.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
// Time: O(m * n)
// Space: O(m)
class Solution {
public:
int findGoodStrings(int n, string s1, string s2, string evil) {
const auto& prefix = getPrefix(evil);
vector<vector<vector<vector<int>>>> dp(
2, vector<vector<vector<int>>>(
2, vector<vector<int>>(
2, vector<int>(evil.length()))));
dp[0][0][0][0] = 1;
for (int i = 0; i < n; ++i) {
dp[(i + 1) % 2] = vector<vector<vector<int>>>(
2, vector<vector<int>>(
2, vector<int>(evil.length())));
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 2; ++k) {
const auto min_c = j ? 'a' : s1[i];
const auto max_c = k ? 'z' : s2[i];
for (int l = 0; l < evil.length(); ++l) {
if (!dp[i % 2][j][k][l]) {
continue;
}
for (char c = min_c; c <= max_c; ++c) {
int m = l - 1;
while (m != -1 && evil[m + 1] != c) {
m = prefix[m];
}
if (evil[m + 1] == c) {
++m;
}
if (m + 1 == evil.length()) {
continue;
}
add(&dp[(i + 1) % 2][j || c != s1[i]][k || c != s2[i]][m + 1], dp[i % 2][j][k][l]);
}
}
}
}
}
int result = 0;
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 2; ++k) {
for (int l = 0; l < evil.length(); ++l) {
add(&result, dp[n % 2][j][k][l]);
}
}
}
return result;
}
private:
vector<int> getPrefix(const string& pattern) {
vector<int> prefix(pattern.length(), -1);
int j = -1;
for (int i = 1; i < pattern.length(); ++i) {
while (j != -1 && pattern[j + 1] != pattern[i]) {
j = prefix[j];
}
if (pattern[j + 1] == pattern[i]) {
++j;
}
prefix[i] = j;
}
return prefix;
}
void add(int *x, int y) {
static const int MOD = 1e9 + 7;
*x = (*x + y) % MOD;
}
};