https://vjudge.net/problem/CodeForces-1132F
You are given a string \(s\) of length \(n\) consisting of lowercase Latin letters. You may apply some operations to this string: in one operation you can delete some contiguous substring of this string, if all letters in the substring you delete are equal. For example, after deleting substring bbbb from string abbbbaccdd we get the string aaccdd.
Calculate the minimum number of operations to delete the whole string \(s\).
The first line contains one integer \(n\) (\(1 \le n \le 500\)) — the length of string \(s\).
The second line contains the string \(s\) (\(|s| = n\)) consisting of lowercase Latin letters.
Output a single integer — the minimal number of operation to delete string \(s\).
5
abaca
3
8
abcddcba
4
区间dp,设\(f[l][r]\)是消除\(l...r\)区间所花的最小次数,对于每次更新:
#include <bits/stdc++.h>
#define N 505
using namespace std;
int max(int a, int b) {
return a > b ? a : b;
}
int min(int a, int b) {
return a < b ? a : b;
}
int f[N][N];
char s[N];
int main() {
int n;
scanf("%d", &n);
scanf("%s", s + 1);
for (int i = 1; i <= n; i++) {
f[i][i] = 1;
}
for (int len = 2; len <= n; len++) {
for (int l = 1; l + len - 1 <= n; l++) {
int r = l + len - 1;
if (s[l] == s[r]) f[l][r] = f[l + 1][r - 1] + 1;
else f[l][r] = min(f[l + 1][r], f[l][r - 1]) + 1;
for (int k = l; k <= r; k++) {
f[l][r] = min(f[l][r], f[l][k] + f[k][r] - 1);
}
}
}
printf("%d\n", f[1][n]);
return 0;
}
DP还是很玄学的啊...
CodeForces-1132F Clear the String
原文:https://www.cnblogs.com/artoriax/p/10495095.html