forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
maximum-product-of-the-length-of-two-palindromic-subsequences.cpp
47 lines (45 loc) · 1.58 KB
/
maximum-product-of-the-length-of-two-palindromic-subsequences.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
// Time: O(3^n)
// Space: O(2^n)
class Solution {
public:
int maxProduct(string s) {
vector<int> dp(1 << size(s));
for (int mask = 0; mask < size(dp); ++mask) {
dp[mask] = palindromic_subsequence_length(s, mask);
}
int result = 0;
for (int mask = 0; mask < size(dp); ++mask) {
if (dp[mask] * (size(s) - dp[mask]) <= result) { // optimize
continue;
}
// submask enumeration:
// => sum(nCr(n, k) * 2^k for k in xrange(n+1)) = (1 + 2)^n = 3^n
// => Time: O(3^n), see https://cp-algorithms.com/algebra/all-submasks.html
const int inverse_mask = (size(dp) - 1) ^ mask;
for (int submask = inverse_mask; submask; submask = (submask - 1) & inverse_mask) {
result = max(result, dp[mask] * dp[submask]);
}
}
return result;
}
private:
int palindromic_subsequence_length(const string& s, int mask) {
int result = 0;
int left = 0, right = size(s) - 1;
int left_bit = 1 << left, right_bit = 1 << right;
while (left <= right) {
if ((mask & left_bit) == 0) {
++left, left_bit <<= 1;
} else if ((mask & right_bit) == 0) {
--right, right_bit >>= 1;
} else if (s[left] == s[right]) {
result += (left == right) ? 1 : 2;
++left, left_bit <<= 1;
--right, right_bit >>= 1;
} else {
return 0;
}
}
return result;
}
};