forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximum-product-of-the-length-of-two-palindromic-substrings.cpp
129 lines (122 loc) · 3.62 KB
/
maximum-product-of-the-length-of-two-palindromic-substrings.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
125
126
127
128
129
// Time: O(n)
// Space: O(n)
class Solution {
public:
long long maxProduct(string s) {
const auto& P = manacher(s);
queue<pair<int64_t, int64_t>> q;
vector<int64_t> left = {0};
for (int i = 0; i < size(s); ++i) {
while (!empty(q) && q.front().second < i) {
q.pop();
}
left.emplace_back(max(left.back(), empty(q) ? 1 : 1 + 2 * (i - q.front().first)));
q.emplace(i, i + P[2 * i + 2] / 2);
}
q = queue<pair<int64_t, int64_t>>();
int64_t result = 0, right = 0;
for (int i = size(s) - 1; i >= 0; --i) {
while (!empty(q) && q.front().second > i) {
q.pop();
}
right = max(right, empty(q) ? 1 : 1 + 2 * (q.front().first - i));
q.emplace(i, i - P[2 * i + 2] / 2);
result = max(result, left[i] * right);
}
return result;
}
private:
vector<int> manacher(const string& s) {
string T = preProcess(s);
const int n = size(T);
vector<int> P(n);
int C = 0, R = 0;
for (int i = 1; i < n - 1; ++i) {
int i_mirror = 2 * C - i;
P[i] = (R > i) ? min(R - i, P[i_mirror]) : 0;
while (T[i + 1 + P[i]] == T[i - 1 - P[i]]) {
++P[i];
}
if (i + P[i] > R) {
C = i;
R = i + P[i];
}
}
return P;
}
string preProcess(const string& s) {
if (empty(s)) {
return "^$";
}
string ret = "^";
for (int i = 0; i < size(s); ++i) {
ret += "#" + s.substr(i, 1);
}
ret += "#$";
return ret;
}
};
// Time: O(n)
// Space: O(n)
class Solution2 {
public:
long long maxProduct(string s) {
const auto& l1 = find_max_len(s);
reverse(begin(s), end(s));
const auto& l2 = find_max_len(s);
int64_t result = 0;
for (int i = 0; i < size(l1); ++i) {
result = max(result, int64_t(l1[i]) * l2[size(l2) - 1 - i]);
}
return result;
}
private:
vector<int> find_max_len(const string& s) {
const auto& P = manacher(s);
vector<pair<int, int>> intervals;
for (int i = 2; i < size(P) - 2; i += 2) {
intervals.emplace_back((i - 2) / 2 - P[i] / 2, (i - 2) / 2 + P[i] / 2);
}
vector<int> dp(size(s));
for (int i = size(intervals) - 1; i >= 0; --i) {
dp[intervals[i].second] = intervals[i].second - intervals[i].first + 1;
}
for (int i = size(intervals) - 2; i >= 0; --i) {
dp[i] = max(dp[i], dp[i + 1] - 2);
}
vector<int> result = {0};
for (int i = 0; i < size(dp); ++i) {
result.emplace_back(max(result.back(), dp[i]));
}
return result;
}
vector<int> manacher(const string& s) {
string T = preProcess(s);
const int n = size(T);
vector<int> P(n);
int C = 0, R = 0;
for (int i = 1; i < n - 1; ++i) {
int i_mirror = 2 * C - i;
P[i] = (R > i) ? min(R - i, P[i_mirror]) : 0;
while (T[i + 1 + P[i]] == T[i - 1 - P[i]]) {
++P[i];
}
if (i + P[i] > R) {
C = i;
R = i + P[i];
}
}
return P;
}
string preProcess(const string& s) {
if (empty(s)) {
return "^$";
}
string ret = "^";
for (int i = 0; i < size(s); ++i) {
ret += "#" + s.substr(i, 1);
}
ret += "#$";
return ret;
}
};