-
Notifications
You must be signed in to change notification settings - Fork 1
/
132.cpp
62 lines (57 loc) · 1.79 KB
/
132.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
// 132. Palindrome Partitioning II - https://leetcode.com/problems/palindrome-partitioning-ii
#include "bits/stdc++.h"
#include "gtest/gtest.h"
using namespace std;
class Solution {
private:
vector<deque<bool>> dp;
void run_is_palindrome_dp(const string &s) {
int n = (int)s.length();
dp.resize(n, deque<bool> (n, false));
for (int idx = 0; idx < n; ++idx) {
dp[idx][idx] = true;
}
for (int idx = 0; idx < n - 1; ++idx) {
dp[idx][idx + 1] = (s[idx] == s[idx + 1]);
}
for (int len = 3; len <= n; ++len) {
for (int start = 0; start + len - 1 < n; ++start) {
int finish = start + len - 1;
dp[start][finish] = (s[start] == s[finish]) ? dp[start + 1][finish - 1] : false;
}
}
}
bool is_palindrome(int L, int R) {
return dp[L][R];
}
public:
int minCut(string s) {
run_is_palindrome_dp(s);
vector<int> result(s.length());
iota(result.begin(), result.end(), 1);
// Let result[idx] be the min cut for str[0:idx].
int L = 0;
for (int R = 0; R < s.length(); ++R) {
if (is_palindrome(L, R)) { result[R] = 0; }
else {
// [L, M - 1] U [M, R]
for (int M = 1; M <= R; ++M) {
if (is_palindrome(M, R)) {
result[R] = min(result[R],
result[M - 1] + 1);
}
}
}
}
return result[s.length() - 1];
}
};
TEST(SolutionTest, Small) {
Solution sol;
EXPECT_EQ(1, sol.minCut("aab"));
}
int main(int argc, char **argv) {
ios::sync_with_stdio(false);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}