'commit'
This commit is contained in:
41
TangDou/CSP-J/J2-2022/LeetCode664_dp.cpp
Normal file
41
TangDou/CSP-J/J2-2022/LeetCode664_dp.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 110;
|
||||
/*
|
||||
https://leetcode.cn/problems/strange-printer/
|
||||
|
||||
输入:s = "aaabbb"
|
||||
输出:2
|
||||
解释:首先打印 "aaa" 然后打印 "bbb"。
|
||||
示例 2:
|
||||
|
||||
输入:s = "aba"
|
||||
输出:2
|
||||
解释:首先打印 "aaa" 然后在第二个位置打印 "b" 覆盖掉原来的字符 'a'。
|
||||
|
||||
*/
|
||||
int f[N][N];
|
||||
|
||||
int main() {
|
||||
string s;
|
||||
|
||||
cin >> s;
|
||||
|
||||
int n = s.size();
|
||||
memset(f, 0x3f, sizeof f);
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
f[i][i] = 1;
|
||||
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
if (s[i] == s[j])
|
||||
f[i][j] = f[i][j - 1];
|
||||
else {
|
||||
for (int k = i; k < j; k++)
|
||||
f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << f[0][n - 1] << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user