题目链接
https://vjudge.net/problem/CodeForces-1132F
题面
Description
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\).
Input
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
Output a single integer — the minimal number of operation to delete string \(s\).
Examples
Input
5
abaca
Output
3
Input
8
abcddcba
Output
4
题解
区间dp,设\(f[l][r]\)是消除\(l...r\)区间所花的最小次数,对于每次更新:
- 如果\(s[l] == s[r]\), \(f[l][r] = f[l+1][r-1]+1\)
- 如果\(s[l] != s[r], f[l][r] = min(f[l + 1][r], f[l][r-1]) + 1\)
- 枚举中间点k,\(f[l][r] = min(f[l][r], f[l][k] + f[k][r] -1)\),这样的话k这个点删了两次,所以要减一,因为\(l, k, r\)可能被同时删去,所以要这样转移
AC代码
#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还是很玄学的啊...