-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path664.cpp
More file actions
20 lines (20 loc) · 693 Bytes
/
664.cpp
File metadata and controls
20 lines (20 loc) · 693 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
int strangePrinter(string s) {
int n = s.size();
vector<vector<int>> dp(n, vector<int>(n, 0));
for (int i = n - 1; i >= 0; --i) {
for (int j = i; j < n; ++j) {
dp[i][j] = (i == j) ? 1 : dp[i + 1][j] + 1;
// e.g.,
// abcdcef => abc[d][cef] suppose ab[cccef] => ab[c[d]cef]
// ^ ^ ^ ^ ^ ^
// i k j i k j
for (int k = i + 1; k <= j; ++k) {
if (s[i] == s[k]) dp[i][j] = min(dp[i][j], dp[i + 1][k - 1] + dp[k][j]);
}
}
}
return dp[0][n - 1];
}
};